You can use the code snippet mvxcmdap to generate an async command. This command is an async command with a generic type Post as its param. When the user clicks the post item, I can get the current post id and pass it into the command.
For the MvvmCrossDemo.Droid project, update the data-binding in the PostListView.axml like this:
Now the ListView is able to respond the ItemClick event and pass the context (the current Post) to the ShowPostDetailAsyncCommand.
For the MvvmCrossDemo.iOS project, it is a little complicated. We need to create a new class called PostListTableSource that inherits from MvxTableViewSource to replace the default MvxStandardTableViewSource, then override the RowSelected method to response the item click event:
[MvxFromStoryboard(nameof(PostListView))]publicpartialclassPostListView:MvxTableViewController<PostListViewModel>{privatePostListTableSource _source;publicPostListView (IntPtr handle) : base (handle) { }publicoverridevoidViewDidLoad() { base.ViewDidLoad(); _source =newPostListTableSource(TableView);TableView.Source= _source;var set =this.CreateBindingSet<PostListView,PostListViewModel>();set.Bind(_source).To(vm =>vm.PostList);set.Apply();TableView.ReloadData(); }}
For the MvvmCrossDemo.Uwp project, we need to install Behaviors package first from the NuGet Package Manager. Or you can input the command below in the Package Manager Console:
You can also use some other InvokeCommandAction to bind the command of the ViewModel, for example, you can set the IsItemClickEnabled property of the ListView as True and bind the command to the ItemClick event. There is not only one approach to achieve the goal, so just select the one you like. Make sure you are clear about the DataContext of your current controls to avoid some unexpected binding errors.