MvvmLight无法为密钥创建控制器

时间:2015-02-18 04:17:37

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

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

Unable to create a controller for key ChartsPage

设置。

在PCL中我有我的ViewModels。我有一个ViewModelLocator设置。我使用mvvmlightlibs Nuget Package。

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

我绝对错过了某处的细节!!!

1 个答案:

答案 0 :(得分:1)

如果你仔细关注博客文章here,它说使用Storyboard你应该使用字符串重载而不是nav.Configure(Key,ViewController)中的typeof()并始终设置storyboardId和restorationId in Storyboard ViewController。

  

请注意,因为我们使用的是Storyboard,所以必须确保使用   配置(字符串,字符串)重载,而不是配置(字符串,   输入一个。