C# – Android data binding between xamarin and MVVM light

Original question:

There was a problem trying to bind the value in my ViewModel to textview in xamarin Android using MVVM light. The value will change once and then stop working. Even if the object changes in ViewModel and the binding mode is set to the default value (oneway)

Due to business needs, I need to continue to use MVVM light and xamarin Android

Example: in my first clip, I have a list of books, which are bound to the list of books in my ViewModel. In my ViewModel, I have an object named currentbook, which changes with the onclick event in my list

VM.CurrentBook = Books[index];

In my second clip, I bound the title to textview

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

When the current book is changed from null to book instance for the first time, the title will be changed as needed. After vm.currentbook = books [index] is changed for the first time; The title is consistent with the first anthology

to update:

I tried several things with the help of milen Pavlov. I tried to change to

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

This triggers an error:

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

Another thing I tried:

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

This raises another error:

System.Reflection.TargetException: Object of type 'Android.Support.V7.Widget.AppCompatTextView' doesn't match target type '[solution].Client.Android.BookDetailsFragment'

resolvent:

I have been using this overload when binding data on xamarin Android and MVVM light:

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

I hope I can help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>