MvvmCross fragments resolving

放肆的年华 提交于 2019-12-12 01:54:39

问题


I'm using Xamarin with MvvmCross, and have problem with fragments usage.

I call ShowViewModel so:

public class MainViewModel : MvxViewModel
{
    public override void Start()
    {
        ShowViewModel<MainMenuViewModel>();
    }
}

Where MainMenuViewModel it's class:

public class MainMenuViewModel : MvxViewModel
{
}

Implemented fragment as follows:

[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: Unable to find explicit activity class {Mvvm.Droid/md5f67dcc55ddb5809d2766dd0c42c8b3bb.MainMenuView}; have you declared this activity in your AndroidManifest.xml?

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

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?


回答1:


The issue is in your MvxFragment attribute:

[MvxFragment(typeof(MainMenuViewModel), Resource.Id.navigation_frame)]

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 description of MvxFragment attribute:

public MvxFragmentAttribute(
    Type parentActivityViewModelType, 
    int fragmentContentId, 
    bool addToBackStack = false); 


来源:https://stackoverflow.com/questions/40287483/mvvmcross-fragments-resolving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!