2
votes

I'm having an error which I quite don't know why I'm getting.

ShellPage.cs:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        fragmentManager = FragmentManager;

        RegisterFragments(bundle);

        .
        .
        .

        ViewModel.ShowMenu();
        ViewModel.ShowFirstContent();
    }

    private void RegisterFragments(Bundle bundle)
    {
        RegisterFragment<MenuContent, MenuContentViewModel>(typeof(MenuContent).Name);
        RegisterFragment<AddChildContent, AddChildContentViewModel>(typeof(AddChildContent).Name);
    }

    public bool Show(MvxViewModelRequest request, Bundle bundle)
    {
        .
        .
        .

        if (request.ViewModelType == typeof(MenuContentViewModel))
        {
            ShowFragment(typeof(MenuContent).Name, Resource.Id.left_drawer, bundle);
            return true;
        }
        else
        {
            ShowFragment(request.ViewModelType.Name, Resource.Id.content_frame, bundle);
            return true;
        }
    }

ShellPageViewModel.cs

    public class ShellPageViewModel : BaseViewModel
    {
        public void ShowMenu()
        {
            ShowViewModel<MenuContentViewModel>();
        }

        public void ShowFirstContent()
        {
            ShowViewModel<SelectChildContentViewModel>();
        }
    }

Basically, when ShowFirstContent() is called, I get the folowing error:

    Cirrious.CrossCore.Exceptions.MvxException: Could not find tag: SelectChildContentViewModel in cache, you need to register it first.

When RegisterFragment() is called from the OnCreate(), it raises no errors, so I assume that it registers the fragment and viewmodel correctly.

Am I doing something wrong?

The code I used is all based on James Montemango's code:

https://github.com/jamesmontemagno/Xam.NavDrawer/tree/master/Material%20(Lollipop%20Style)/MvvmCross

1

1 Answers

2
votes

The code in there has some critical bugs. I would recommend looking into: https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples

The code used there is:

    private void RegisterForDetailsRequests(Bundle bundle)
    {
        RegisterFragment<MenuFragment, MenuViewModel>(typeof(MenuViewModel).Name, bundle);
        RegisterFragment<ExamplesFragment, ExamplesViewModel>(typeof(ExamplesViewModel).Name, bundle);
        RegisterFragment<SettingsFragment, SettingsViewModel>(typeof(SettingsViewModel).Name, bundle);
    }

    public void RegisterFragment<TFragment, TViewModel>(string tag, Bundle args)
        where TFragment : IMvxFragmentView
        where TViewModel : IMvxViewModel
    {
        var customPresenter = Mvx.Resolve<IMvxFragmentsPresenter>();
        customPresenter.RegisterViewModelAtHost<TViewModel>(this);
        RegisterFragment<TFragment, TViewModel>(tag);
    }