0
votes

The following example uses an MVVM approach to update a property based on a picker selection:

https://github.com/xamarin/xamarin-forms-samples/blob/master/UserInterface/BindablePicker/BindablePicker/BindablePicker/SimpleColorPickerPageViewModel.cs

It uses OnPropertyChanged("SelectedColor"); when the selection changes to update another property.

I have two questions:

Firstly, is there any advantage of using this approach over the normal OnSelectedIndexChanged event in the xaml.cs?

Secondly how would i use this approach if i wanted to update a particular item for my custom object in my viewmodel. For example, if the selected item changed in the above example, store the value in another object in the view model?

It just seems long winded to the OnSelectedIndexChanged approach, but guessing there are advantages i'm unaware of?

basicaly i have a number of dropdowns on a page, and with each change, i want to update the object in my view model which will be sent back to the server with a rest service.

1

1 Answers

1
votes

Picker.SelectedIndexChanged is not as straigthforward since you will still need to index the list and find which item and since it's not a command, you need to implement it in Code Behind.

You just said that you need to send rest http requests, so it means that your app is fairly complex so you will probably need ViewModels and to keep your code coherent, you should apply the same pattern to all pages.

For the sake of keeping things neat and tidy I always implement MVVM even if the Page logic is not very extensive.

MVVM also facilitates (a lot) unit testing your application.

But in the end, it's all about taste.