c# - 在 MvxFragment 中通过 Id 查找

标签 c# android android-fragments mvvmcross

我需要在 MvxFragment 中找到我在 axml 中声明的 View 。在 Activity 中我可以调用 FindViewById<T>(RESOURCE_ID)OnCreate .这在 fragment 中不起作用。

这是它与普通 Android fragment 一起工作的方式:findViewById in Fragment

如何在 MvxFragment 中膨胀 View 不丢失绑定(bind)上下文?

最佳答案

它是这样工作的:

您需要了解 EnsureBindingContextIsSet()BindingInflate() 这两个扩展方法。

public class MyFragment : MvxFragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
    {
        this.EnsureBindingContextIsSet(savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.YOUR_VIEW, container, false);
        var searchField = view.FindViewById<T>(Resource.Id.RESOURCE_ID);


        return view;
    }
}

关于c# - 在 MvxFragment 中通过 Id 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30211488/

相关文章:

c# - 从 64 位 exe 访问 32 位 DLL 的方法

android - android :layout_above ="@android:id/tabs"? 的等效代码是什么

java - 如何测试 Android 上的预测键盘是否被禁用?

android - 使用 ListFragment 时无法引用 ListView

c# - 如何从对象列表创建 MVC HtmlHelper 表

c# - 二维数组索引问题

java - FloatingActionButton 单击事件不起作用

android-fragments - 在Kotlin中的片段中调用setSupportActionBar时出错

java - android中的 fragment ,应用程序未安装

c# - 如何设置 SelectionMode 为 multiple 的 Listpicker 项目