我正在使用具有多个ComboBox和GridView的拖放Xaml Uwp应用程序。我在xaml代码中尝试了一下,直到我认为我知道我在应用程序的哪个方向。然后我开始将我的逻辑移动到ViewModel,PlayPageViewModel,我正在使用MvvM Light和模板10.我有许多使用交互的事件。当我将它移到视图模型时,我已经让Drop工作在codeBehind Cannot find method named GvNewPlayList_OnDrop on object of type TheLolFx.UWP.T10.ViewModels.PlayPageViewModel that matches the expected signature.
异常
Exception {System.ArgumentException: Cannot find method named GvNewPlayList_OnDrop on object of type TheLolFx.UWP.T10.ViewModels.PlayPageViewModel that matches the expected signature.
at Microsoft.Xaml.Interactions.Core.CallMethodAction.Execute(Object sender, Object parameter)
at Microsoft.Xaml.Interactivity.Interaction.ExecuteActions(Object sender, ActionCollection actions, Object parameter)
at Microsoft.Xaml.Interactions.Core.EventTriggerBehavior.OnEvent(Object sender, Object eventArgs)} System.Exception {System.ArgumentException}
消息
Message "System.ArgumentException: Cannot find method named GvNewPlayList_OnDrop on object of type TheLolFx.UWP.T10.ViewModels.PlayPageViewModel that matches the expected signature.\r\n at Microsoft.Xaml.Interactions.Core.CallMethodAction.Execute(Object sender, Object parameter)\r\n at Microsoft.Xaml.Interactivity.Interaction.ExecuteActions(Object sender, ActionCollection actions, Object parameter)\r\n at Microsoft.Xaml.Interactions.Core.EventTriggerBehavior.OnEvent(Object sender, Object eventArgs)" string
我使用了后面代码中生成的签名。在ViewModel中触发了ContainerContentChanging
正如您所看到的那样,我也尝试使用了对象。
什么是正确的签名?
XAML
<GridView x:Name="GvNewPlayList"
RelativePanel.Below="CbPlayListEditor"
Visibility="{Binding LbNewPlayListVisibility}"
Background="BurlyWood"
Padding="5"
Header="New Play List"
ItemsSource="{Binding NewLocalSoundsPlayListsSelectedItem.LocalSfxV2s}"
CanDragItems="True"
AllowDrop="True"
CanReorderItems="True"
IsItemClickEnabled="True"
DragItemsStarting="LbNewPlayList_OnDragItemsStarting"
DragOver="LbNewPlayList_OnDragOver">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Drop">
<core:CallMethodAction MethodName="GvNewPlayList_OnDrop"
TargetObject="{Binding Mode=OneWay}" />
</core:EventTriggerBehavior>
<core:EventTriggerBehavior EventName="ContainerContentChanging">
<core:CallMethodAction MethodName="GvLocalSoundsPlayListEditorContainerContentChangingAsync"
TargetObject="{Binding Mode=OneWay}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<GridView.ItemTemplate>
<DataTemplate>
[...]
PlayPageViewModel
在尝试在虚拟机中定位此方法的XAML上触发了异常。
// private async void GvNewPlayList_OnDrop(object sender, object e)
// private async void GvNewPlayList_OnDrop()
private async void GvNewPlayList_OnDrop(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
Logger.Log(this, "yup");
[...]
}
这是来自ContainerContentChanging
事件。
public async void GvLocalSoundsPlayListEditorContainerContentChangingAsync()
{
Logger.Log(this, $"GvLocalSoundsPlayListEditorContainerContentChangingAsync: {SelectedPlayList?.PlayListName}");
//_settings.CurrentPlayList = SelectedPlayList;
//LocalSounds = await _theLolFxV2DataServices.GetPlayListAsync(SelectedPlayList);
//NewLocalSoundsPlayListItems = await _theLolFxV2DataServices.GetPlayListAsync(CbPlayListEditorSelectedItem);
}
答案 0 :(得分:1)
使用CallMethodAction调用方法时,方法的签名应如下所示:public void DoSomthing()
。异常的原因是:
GvNewPlayList_OnDrop标记为private
,需要标记为public
;
它不能包含任何参数。
所以只需像第二种方法一样修改其签名:public async void GvLocalSoundsPlayListEditorContainerContentChangingAsync()