Xamarin Android Data-Binding with MVVM backlight

The original question:

I have a problem linking a value from my ViewModel to a TextView in Xamarin Android using MVVM Light. The value changes once, and then stops working. even if the object is changed in the ViewModel and the snap mode is set by default (OneWay).

Due to business requirements, I must continue to use MVVM Light and Xamarin Android.

Example:
In my first snippet, I have a list of books that are tied to a list of books in my ViewModel. In my Viewmodel, I have an object called CurrentBook that changes with the onclick event in my list.

VM.CurrentBook = Books[index];

In my second snippet, I have a title associated with a TextView

_titleBinding = this.SetBinding(() => VM.CurrentBook.Title, () => TitleTextView.Text);

The first time the current book is changed from NULL to an instance of the Book , the name changes as desired. After the first time when changing VM.CurrentBook = Books [index]; the title remains the same as the first book with the selector.


Update:

I tried a couple of things with the help of Milen Pavlov, I tried to switch to

VM.SetBinding(() => VM.CurrentBook.Title, TitleTextView, () => TitleTextView.Text, BindingMode.TwoWay);

This causes an error:

System.Reflection.TargetException: Object of type '[Solution].Client.Shared.ViewModels.BooksViewModel' doesn't match target type '[Solution].Client.Android.BookDetailsFragment'



One more thing I tried:

_titleBinding = this.SetBinding(() => VM.CurrentBook.Title, TitleTextView, () => TitleTextView.Text, BindingMode.TwoWay);
enter code here

This causes another error:

System.Reflection.TargetException: Object of type 'Android.Support.V7.Widget.AppCompatTextView' doesn't match target type '[solution].Client.Android.BookDetailsFragment'
+4
source share
1 answer

I used this overload when binding data to xamarin android and mvvm-light:

VM.SetBinding(() => VM.CurrentBook.Title, TitleTextView, () => TitleTextView.Text, BindingMode.TwoWay); 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1627264/


All Articles