Xamarin MvvmCross MvxAppCompatDialogFragment松开BindingContext [英] Xamarin MvvmCross MvxAppCompatDialogFragment looses BindingContext
问题描述
我在整个项目中一直使用MvxAppCompatActivity
,但是在这种情况下,我必须使用MvxAppCompatDialogFragment
.
I've been using MvxAppCompatActivity
throughout my project, but in this specific case I have to make use of a MvxAppCompatDialogFragment
.
不幸的是,在这种情况下,我以某种方式丢失了ViewModel的绑定上下文.
Unfortunately, in this case I lose the binding context of the ViewModel somehow.
MobileTestView
[MvxDialogFragmentPresentation]
[Register(nameof(MobileScreenTestView))]
public class MobileTestView : MvxAppCompatDialogFragment<MobileTestViewModel>
...
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.mobile_screen, container, false);
}
...
MobileTestViewModel
public class MobileTestViewModel : MvxViewModel<MInput, MResult>
...
public string Instructions { get; set; } = "Instructions";
...
mobile_screen.axml
...
<TextView
android:id="@+id/text_mobile"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|center_horizontal"
tools:text="Scan"
local:MvxBind="Text Instructions" />
...
local:MvxBind="Text Instructions"
不再起作用,但是我已经检查了它,并在进入OnCreateView()
之前在视图模型中对其进行了设置.
local:MvxBind="Text Instructions"
does not work anymore, but I've checked and it is set in the view model before it gets to OnCreateView()
.
上面的代码对于MvxAppCompatActivity
可以正常工作.
The above code would work fine for a MvxAppCompatActivity
.
如果我想做的事情不可能完成,我总是可以这样做
If what I'm trying to do is not possible, I can always do it like
view.FindViewById<TextView>(Resource.Id.text_mobile).Text = ViewModel.Instructions;
并不是我真的需要使用local:MvxBind
,但是我想知道我在做什么错.
It's not that I really need to use local:MvxBind
, but I would like to know what I'm doing wrong.
更新-对于存在相同问题的任何人:
Update - For anyone having the same problem:
将OnCreateView
方法更改为:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.mobile_screen, container, false);
}
,并且您的BindingContext可以正常工作.
and your BindingContext will work fine.
推荐答案
您已经注意到自己,必须使用this.BindingInflate
而不是OnCreateView
中的LayoutInflater
参数.这是因为我们无法在MvvmCross中拦截Fragment生命周期来提供自己的Layout Inflater.
As you noticed yourself, you had to use this.BindingInflate
instead of the LayoutInflater
argument in OnCreateView
. This is because we have no way to intercept the Fragment lifecycle in MvvmCross to provide our own Layout Inflater.
BindingInflate的作用是遍历视图层次结构,查找适用于视图的所有自定义属性,在您的情况下,请按Text Instructions
并在View和ViewModel之间应用这些绑定.
What BindingInflate does, is to run through the view hierarchy and look for all the custom attributes applied on views, in your case Text Instructions
and apply these bindings between the View and the ViewModel.
因此,每当使用Fragments时,都应使用BindingInflate.
So whenever working with Fragments, you should use BindingInflate.
这篇关于Xamarin MvvmCross MvxAppCompatDialogFragment松开BindingContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!