如何将EventArgs传递给DelegateCommand?

Lou*_*ley 1 c# prism mvvm xamarin xamarin.forms

我有一个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; }
}
Run Code Online (Sandbox Code Playgroud)

XAML部分:

  <mobile:CustomCocosSharpView>
    <behaviors:Interaction.Behaviors>
      <behaviors:BehaviorCollection>
        <behaviors:EventToCommand EventName="OnTouched"
                                  Command="{Binding OnTouchedCommand}" />        
      </behaviors:BehaviorCollection>
    </behaviors:Interaction.Behaviors>
  </mobile:CustomCocosSharpView>
Run Code Online (Sandbox Code Playgroud)

最后是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?
            }));
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题:

如何CustomEventArgs在DelegateCommand中获取参数?一定是可能的!但没有任何作用:/

感谢帮助!

Bri*_*nas 5

如果您使用的是Prism 6.3-pre2,则可以使用内置的EventToCommand,并使用转换器或路径完全控制传递给DelegateCommand的内容.你可以在这里查看文档:http: //prismlibrary.readthedocs.io/en/latest/Xamarin-Forms/6-EventToCommandBehavior/#using-the-eventtocommandbehavior