xamarin - MvvmCross - 遇到 ViewModel 注册问题

标签 xamarin xamarin.android mvvmcross

我遇到了一个错误,我完全不知道为什么会出现这个错误。

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

基本上,当调用 ShowFirstContent() 时,我收到以下错误:

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

当从 OnCreate() 调用 RegisterFragment() 时,它不会引发任何错误,因此我假设它正确注册了片段和 View 模型。

我做错了什么吗?

我使用的代码全部基于James Montemango的代码:

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

最佳答案

其中的代码有一些严重的错误。我建议查看:https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples

那里使用的代码是:

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

关于xamarin - MvvmCross - 遇到 ViewModel 注册问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996733/

相关文章:

ios - MvvmCross iOS : How to bind MapView Annotation to jump to another view?

c# - 存档的 APK 文件大小非常巨大 - Xamarin.Android

具有 multidex 的 Android 应用程序(Xamarin Forms) - 启动如此缓慢

单色触摸 : Namespaces allowed

xamarin.ios - 使用 MVVMCross 绑定(bind)错误信息

c# - DequeueReusableCell 仅在 iPhone 4S 上使应用程序崩溃

xamarin - Sharpie 绑定(bind) Objective-C @protocols 问题

c# - 如何在 Xamarin Forms 上获取当前设备型号?

c# - 如何在android应用程序中使用持久数据库

c# - 如何在 Xamarin.Forms 中处理 WCF Duplex?