Search code examples
androidxamarinmvvmcross

MvvmCross Android ViewStub


I recently found a class called ViewStub that can be used to "lazy-load" a layout-resource. The usage is very straight forward:

In the layout file I use:

<ViewStub
    android:id="@+id/content_stub"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

And in my code:

var stub = this.FindViewById<ViewStub>(Resource.Id.content_stub);
stub.LayoutResource = Resource.Layout.FirstView;
stub.Inflate();

However, this way the Bindings won't work! I know, that using mvvmcross I have to call BindingInflate, but that method is not available for a ViewStub I looked for something like MvxViewStub, but I couldn't find anything.

So, is there a way to somehow get the binding work with the ViewStub? Thanks for your help.


Solution

  • After crawling through various mvvmcross-sources, I found a solution that seems to work. I'm not shure if thats the way how it should be done, so if someone has a better approach, please tell me.

    How it works for now:

    using (new MvxBindingContextStackRegistration<IMvxAndroidBindingContext>((IMvxAndroidBindingContext)this.BindingContext))
    {
        var stub = this.FindViewById<ViewStub>(Resource.Id.content_stub);
        stub.LayoutInflater = this.LayoutInflater;
        stub.LayoutResource = Resource.Layout.FirstView;
        stub.Inflate();
    }