简体   繁体   中英

Xamarin Android Data-Binding with MVVM Light

Original Question:

I have a problem trying to bind a value from my ViewModel to a TextView in Xamarin Android using MVVM Light. The value changes once and then stops working. even though the object changes in the ViewModel and the binding mode is set to the default (OneWay).

Due to business requirements i am required to keep using MVVM Light and Xamarin Android.

Example:
In my first fragment I have a list of books that is bound to a list of books in my ViewModel. In my Viewmodel i have an object named CurrentBook which changes with an onclick event in my list.

VM.CurrentBook = Books[index];

In my second fragment i have the title bound to a TextView

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

The first time the Current book changes from NULL to an instance of Book the title changes as desired. After the first time when changing VM.CurrentBook = Books[index]; the title remains the same as the first seleced book.


Update:

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

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

This triggerd 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 triggerd another error:

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

I've been using this overload when data binding on xamarin android and mvvm-light:

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM