【问题标题】:MvvmLight unable to create a controller for keyMvvmLight 无法为密钥创建控制器
【发布时间】:2015-04-18 23:57:06
【问题描述】:

我正在使用 Xamarin iOS 和 Xamarin Android 设计一个跨平台的应用程序架构,我决定使用 MvvmLight,它看起来很下降,并且没有隐藏 MVVM 模式的所有内容,既好又灵活。 虽然尝试设置它并学习如何使用它开始变得有意义,但我发现自己很难理解为什么会出现以下错误。

Unable to create a controller for key ChartsPage

设置。

在 PCL 中,我有我的 ViewModel。我有一个 ViewModelLocator 设置。我使用 mvvmlightlibs Nuget 包。

public class ViewModelLocator
    {
        public static readonly string SchedulerPageKey = @"SchedulerPage";
        public static readonly string ChartsPageKey = @"ChartsPage";

        [SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SchedulerViewModel Scheduler
        {
            get
            {
                return ServiceLocator.Current.GetInstance<SchedulerViewModel>();
            }
        }

        public BizchartsViewModel Bizcharts
        {
            get
            {
                return ServiceLocator.Current.GetInstance<BizchartsViewModel>();
            }
        }

        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
//              Haven't declared something yet
            }
            else
            {
//              Haven't declared something yet
            }

            SimpleIoc.Default.Register<SchedulerViewModel>();
            SimpleIoc.Default.Register<BizchartsViewModel>();
        }
    }

我有一个统一的 iOS 应用程序,它使用具有大小类的通用故事板,它有一个初始 UINavigationViewController SchedulerViewController,在 ViewDidLoad 方法中,我测试到 BizchartsViewController 的导航延迟 3 秒。 3 秒后,我得到了异常。

在 AppDelegate 中。

private static ViewModelLocator _locator;
        public static ViewModelLocator Locator
        {
            get
            {
                if (_locator == null)
                {
                    SimpleIoc.Default.Register<IDialogService, DialogService>();

                    _locator = new ViewModelLocator();
                }

                return _locator;
            }
        }
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            var nav = new NavigationService();
            nav.Initialize((UINavigationController)Window.RootViewController);
            nav.Configure(ViewModelLocator.ChartsPageKey, typeof(BizchartsViewController));

            SimpleIoc.Default.Register<INavigationService>(() => nav);

            return true;
        }

SchedulerViewController

partial class SchedulerViewController : UIViewController
    {
        public SchedulerViewModel Vm {
            get;
            private set;
        }

        public SchedulerViewController (IntPtr handle) : base (handle)
        {
            Vm = AppDelegate.Locator.Scheduler;
        }

        public async override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            await Task.Delay (3000);
            Vm.NavigateToCharts ();
        }
    }

SchedulerViewModel

public class SchedulerViewModel : ViewModelBase
    {
        public void NavigateToCharts()
        {
            var nav = ServiceLocator.Current.GetInstance<INavigationService>();
            nav.NavigateTo(ViewModelLocator.ChartsPageKey);
        }
    }

我肯定错过了某个地方的细节!!!

【问题讨论】:

    标签: ios mvvm xamarin.ios xamarin mvvm-light


    【解决方案1】:

    如果您仔细阅读博客文章here,它说使用 Storyboard,您应该在 nav.Configure(Key, ViewController) 中使用字符串重载而不是 typeof() 并始终在 Storyboard 中设置 storyboardId 和 restoreId视图控制器。

    请注意,由于我们使用的是 Storyboard,因此您必须确保使用 Configure(string, string) 重载,而不是 Configure(string, 输入)一个。

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2018-08-19
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多