MvvmCross Android ViewStub [英] MvvmCross Android ViewStub

查看:58
本文介绍了MvvmCross Android ViewStub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了一个名为 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"/>

在我的代码中:

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

但是,这样绑定将不起作用!我知道,使用 mvvmcross 我必须调用 BindingInflate,但是该方法不适用于 ViewStub 我寻找类似 MvxViewStub 的东西,但是我找不到任何东西.

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.

那么,有没有办法以某种方式使 ViewStub 绑定工作?感谢您的帮助.

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

推荐答案

在浏览各种 mvvmcross-sources 后,我找到了一个似乎有效的解决方案.我不确定是否应该这样做,所以如果有人有更好的方法,请告诉我.

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.

目前的工作原理:

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();
}

这篇关于MvvmCross Android ViewStub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆