I'm using MvvmCross v3.06 and I've defined a MvxListView in android which is bound to a list. I can see the list but can't work out the best way to get hold of the item that is selected when I click it.
At the moment I'm doing the following in the OnCreate of the activity but it's not particularly MVVM and I wondered if there is a better way through binding?
var list = FindViewById<MvxListView>(Resource.Id.subslist);
list.ItemClick = ((MyViewModel)ViewModel).ItemSelectedCommand;
I can't find any documentation on the best way to go about this so any help would be great.
With the method binding plugin, MvvmCross data binding can use ViewModel public methods as sources for ICommand without declaring an ICommand property. can be used in binding as:
MvvmCross is a library to enable usage of the Mvvm pattern in Xamarin apps when targeting iOS and Android. With this library it’s really easy to share viewmodels between the apps instead of creating separate for each platform. It also enables bindings on Android and iOS.
MvvmCross to the rescue! MvvmCross is a cross-platform MVVM library that allows you to put a common PCL directly from xamarin.android and xamrin.ios. And you get most of the Mvvm data binding to view sugars around this too.
Xamarin comes both as a standalone studio or you could use Visual Studio. MvvmCross is a library to enable usage of the Mvvm pattern in Xamarin apps when targeting iOS and Android. With this library it’s really easy to share viewmodels between the apps instead of creating separate for each platform.
For android, the most common technique is to bind ItemClick
to an MvxCommand<TItem>
- so use:
local:MvxBind="ItemClick ItemSelectedCommand"
You can see this in operation in examples including:
Less common (so far), for Android, you can also bind to a custom binding SelectedItem
on MvxListView
This technique is shown on a Spinner
(MvxSpinner
) in MoreControls - https://github.com/slodge/MvvmCross-Tutorials/blob/master/MoreControls/MoreControls.Droid/Resources/Layout/FirstView.axml (this example is constructed live during the N=18 video - see http://youtu.be/s1LhXdCTsn4?t=7m26s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With