【发布时间】:2017-08-16 23:59:54
【问题描述】:
我有一个Xamarin.Forms 项目。在 Prism 中作为 MVVM 框架。我有一个自定义控件(源自CocosSharpView,但这并不重要)。我正在使用参数在该类中引发自定义事件,但无法将此参数传输到 ViewModel。代码如下:
查看部分。我正在启动一个带有一些参数的 custom 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 参数? 一定可以!但没有任何效果:/
感谢您的帮助!
【问题讨论】:
-
我不确定我是否得到了你想要的东西......但是我认为你是在询问带参数的命令。有一篇关于我刚刚发现的好文章 blog.xamarin.com/simplifying-events-with-commanding
标签: c# xamarin mvvm xamarin.forms prism