我有一个Xamarin.Forms
项目。在Prism中作为MVVM框架。我有一个自定义控件(派生自CocosSharpView
,但这并不重要)。我正在使用参数在该类中上升自定义事件,但无法将此参数传递给ViewModel。这是代码:
查看部分。我正在使用一些参数启动自定义 OnTouched事件:
public class CustomCocosSharpView : CocosSharpView
{
public event EventHandler<CustomEventArgs> OnTouched;
public CCGameView gameView;
// ... not important stuff ...
private void OnViewCreated(object sender, EventArgs ea)
{
if (gameView == null)
{
gameView = sender as CCGameView;
if (gameView != null)
{
_gameScene = new GameScene(gameView);
_gameScene.OnTouched += (s, e) =>
{
CustomEventArgs custom = new CustomEventArgs() { Foo = 4 };
OnTouched?.Invoke(s, custom);
};
gameView.RunWithScene(_gameScene);
}
}
OnCreated?.Invoke(sender, ea);
}
}
public class CustomEventArgs : EventArgs
{
public int Foo { get; set; }
}
XAML 部分:
<mobile:CustomCocosSharpView>
<behaviors:Interaction.Behaviors>
<behaviors:BehaviorCollection>
<behaviors:EventToCommand EventName="OnTouched"
Command="{Binding OnTouchedCommand}" />
</behaviors:BehaviorCollection>
</behaviors:Interaction.Behaviors>
</mobile:CustomCocosSharpView>
最后 ViewModel :
private DelegateCommand<CustomEventArgs> onTouchedCommand;
public DelegateCommand<CustomEventArgs> OnTouchedCommand
{
get
{
return onTouchedCommand ?? (onTouchedCommand = new DelegateCommand<CustomEventArgs>((arg) =>
{
Debug.WriteLine("OnTouchCommand " + arg?.Foo.ToString()); //arg is null. Why?
}));
}
}
问题:
如何在DelegateCommand中获取CustomEventArgs
参数?必须可以!但没有任何作用:/
感谢您的帮助!
答案 0 :(得分:5)
如果您使用的是Prism 6.3-pre2,您可以使用内置的EventToCommand,并使用转换器或路径完全控制传递给DelegateCommand的内容。您可以在此处查看文档:{{3}}