MvvmCross片段解析 [英] MvvmCross fragments resolving
问题描述
我将Xamarin与MvvmCross一起使用,并且在片段使用方面遇到问题.
I'm using Xamarin with MvvmCross, and have problem with fragments usage.
我这样称呼ShowViewModel:
I call ShowViewModel so:
public class MainViewModel : MvxViewModel
{
public override void Start()
{
ShowViewModel<MainMenuViewModel>();
}
}
MainMenuViewModel所在的类:
Where MainMenuViewModel it's class:
public class MainMenuViewModel : MvxViewModel
{
}
实现的片段如下:
[MvxFragment(typeof(MainMenuViewModel), Resource.Id.navigation_frame)]
[Register("mvvm.droid.views.MainMenuView")]
public class MainMenuView : MvxFragment<MainMenuViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.MainMenuView, null);
}
}
但是在运行时会引发错误:
But on runtime it throws error:
Android.Content.ActivityNotFoundException:无法找到显式 活动课 {Mvvm.Droid/md5f67dcc55ddb5809d2766dd0c42c8b3bb.MainMenuView}; 您是否在AndroidManifest.xml中声明了此活动?
Android.Content.ActivityNotFoundException: Unable to find explicit activity class {Mvvm.Droid/md5f67dcc55ddb5809d2766dd0c42c8b3bb.MainMenuView}; have you declared this activity in your AndroidManifest.xml?
为了弄清楚这一点,我实现了CustomPresenter,取自
For figuring out this, i implemented CustomPresenter, taken from here.
然后在安装程序中注册了此演示者的片段:
And in Setup registered this presenter for fragments:
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var mvxFragmentsPresenter = new MvxCustomFragmentsPresenter(AndroidViewAssemblies);
Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
return mvxFragmentsPresenter;
}
似乎主持人发现了片段,但在Show(Intent)方法调用中,它仍在崩溃.在反编译的源代码中,有一个奇怪的检查是否是活动. 尝试基于许多实现来实现抽屉布局,但是结果相同.我想念的是什么?
It seems like presenter found fragments, but at Show(Intent) method call it's still crushing. In decompiled sources there is a strange check if it's an activity. Tryed to implement drawerLayout based on many implementations, but the same result. What i'm missing?
推荐答案
问题出在您的MvxFragment
属性中:
[MvxFragment(typeof( MainMenuViewModel ),Resource.Id.navigation_frame)]
[MvxFragment(typeof(MainMenuViewModel), Resource.Id.navigation_frame)]
第一个参数必须是与要放置菜单片段的Activity相关的MvxViewModel
.在您的情况下,我相信这可能是MainViewModel
?
The first parameter needs to be the MvxViewModel
associated to your Activity that you want to place the menu fragment in. In your case I believe this may be MainViewModel
?
Mvvmcross 描述的MvxFragment属性:
Mvvmcross description of MvxFragment attribute:
public MvxFragmentAttribute(
Type parentActivityViewModelType,
int fragmentContentId,
bool addToBackStack = false);
这篇关于MvvmCross片段解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!