Search code examples
xamarinxamarin.androidmvvmcross

Mvvmcross Visibility in fluent binding


I have got EditText with swiss binding (last line):

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:MvxBind="Visibility Visibility(WebServiceEditVisible); Text EditTextWebServiceAdress" />

How can I write binding to Visibility plugin and to Text property using Fluent binding?


Solution

  • The fluent binding equivalent to the Swiss binding you have implemented would be:

    Resource file:

    <EditText
        android:id="@+id/your_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    Codebehind:

    var myEditText = view.FindViewById<EditText>(Resource.Id.your_edit_text);
    var bindingSet = this.CreateBindingSet<YourView, YourViewModel>();
    
    bindingSet.Bind(yourEditText).For(c => c.Text).To(vm => 
    vm.EditTextWebServiceAdress);
    bindingSet.Bind(yourEditText).For(c => c.Visibility).To(vm => 
    vm.WebServiceEditVisible).WithConversion(new MvxVisibilityValueConverter());
    
    bindingSet.Apply();
    

    This code would go in the OnCreateView override of your fragment. If you're working inside an activity, it would go inside OnCreate instead, and you'd need to change view.FindViewById<EditText>(Resource.Id.your_edit_text); to FindViewById<EditText>(Resource.Id.your_edit_text);.