4

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'

1 Answer 1

0

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.

13
  • after trying this i got this error: System.Reflection.TargetException: Object of type 'Android.Support.V7.Widget.AppCompatTextView' doesn't match target type 'Sollution.Android.BookDetailsFragment'
    – Jonathan
    Feb 4, 2016 at 11:39
  • Are you calling this after inflater.Inflate(..) in OnCreateView()?
    – Milen
    Feb 4, 2016 at 11:42
  • I still have the same error even after placing the binding on the vm System.Reflection.TargetException: Object of type 'sollution.Client.ViewModels.VM' doesn't match target type 'Sollution.Android.BookDetailsFragment'
    – Jonathan
    Feb 4, 2016 at 11:51
  • is the type of sollution.Client.ViewModels.VM different from the type of ViewModel you have in Sollution.Android.BookDetailsFragment?
    – Milen
    Feb 4, 2016 at 11:57
  • the VM is of type BookViewModel which inherits from ViewModelBase and Sollution.Android.BookDetailsFragment is of type BookDetailsFragment which inherits from Fragment
    – Jonathan
    Feb 4, 2016 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.