MVVMCross for android - how to do binding in code?
Asked Answered
W

1

8

I want to use MVVMCross, however for my android application I also want to use other libraries (sliding menu and action bar) which require me to inherit my activity classes from their custom class. This prevents me from inheriting MvxActivity, but I noticed that in MVVMCross for iOS, you can do all your bindings in code (see https://github.com/slodge/NPlus1DaysOfMvvmCross/blob/master/N-00-FirstDemo/FirstDemo.Touch/Views/FirstView.cs)

var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(textEditFirst).To(vm => vm.FirstName);
set.Bind(textEditSecond).To(vm => vm.LastName);
set.Bind(labelFull).To(vm => vm.FullName);
set.Apply();

Is there any way to do that in Android?

Williamswilliamsburg answered 23/5, 2013 at 21:34 Comment(0)
O
11

Yes - you can use fluent bindings in Android if you want to.

Exactly the same code should work.

You'll need to get references to the ui controls using FindViewById<Type>(), then you can bind them.

For example, in TipCalc you can add identified controls like:

<EditText
    android:id="@+id/FluentEdit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:textSize="24dp"
    android:gravity="right"
    />

and then implement binding using:

protected override void OnViewModelSet()
{
    SetContentView(Resource.Layout.View_Tip);

    var edit = this.FindViewById<EditText>(Resource.Id.FluentEdit);

    var set = this.CreateBindingSet<TipView, TipViewModel>();
    set.Bind(edit).To(vm => vm.SubTotal);
    set.Apply();

    // for non-default properties use 'For':
    // set.Bind(edit).For(ed => ed.Text).To(vm => vm.SubTotal);

    // you can also use:
    //   .WithConversion("converter", "optional parameter")
    //   .OneTime(), .OneWay() or .TwoWay()
}

Additionally, you can convert any FooActivity into a data-binding MvxFooActivity by:

  • inheriting from FooActivity to provide events from lifetime events in an EventSourceFooActivity
  • inheriting from EventSourceFooActivity to provide a datacontext in an MvxFooActivity
  • you can then write your code inside activities inheriting from MvxFooActivity

To see, the code required, see:

You'll see the same code in all the mvx adapted Activities - MvxActivity, MvxTabActivity, ... There is a little cut-and-paste here, but as much code as possible is place in shared extension methods.

In previous versions, people have used this technique to bind monogame and google ads activities - eg see Insert a Monogame view inside MvvmCross monodroid Activity

Octosyllabic answered 24/5, 2013 at 6:35 Comment(2)
Hi Stuart, I'm trying the 1st method, but it's giving null ref exception on the Bind() call. I inherit my activity from Activity, IMvxBindingContextOwner, IMvxLayoutInflater, IMvxView and create the binding context and viewmodel myself. Note that since I can't inherit from MvxActivity, I can't call this.CreateBindingSet, I also have to create the MvxAndroidBindingContext myself. Any idea what's wrong? My attempt to see what's going on by compiling the MVVMCross source code also failed, VS says it could not load system.windows.dll assembly, although I removed the ref in Cirrious.CrossCoreWilliamswilliamsburg
Way too many questions/comments in that last comment. I'll change 'alternatively' in my answer to additionally. For everything else in that comment, I'm not getting into a comment conversation - stackoverflow frowns on these.Octosyllabic

© 2022 - 2024 — McMap. All rights reserved.