RelayCommand parameter passing in Xamarin

后端 未结 2 1593
无人共我
无人共我 2021-01-14 05:12

I am very new to Xamarin cross-platform and while I did have some experience with WPF and MVVM I am still having issue understanding p

2条回答
  •  清歌不尽
    2021-01-14 05:34

    I believe you are getting events and commands confused. Some of the difference between the two are that you need to subscribe to events and events must occur. Commands can be called by anyone and also have the ability to be blocked.

    So to get you example to work correctly you should modify your code to allow your RelayCommand to take an action with a parameter. This parameter will define the Type of the parameter. I would use something like MVVMLight which contains a Generic RelayCommand so that you don't have to write your own. Once that is done you should be able to change your code to look like this.

     OpenMenuItemCommand = new RelayCommand(OpenMenuItem);
      ...
      public void OpenMenuItem(MenuItem item)
      {
      }
    

    I wrote a small blog post that contains a full working project if you want to see a working example.

提交回复
热议问题