Search code examples
androidandroid-fragmentsxamarinxamarin.androidmvvmcross

MvvmCross fragments resolving


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?


Solution

  • 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);