MvvmLight 无法为键创建控制器
MvvmLight unable to create a controller for key
我正在使用 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);
}
}
我肯定漏掉了某个地方的细节!!!
如果你仔细阅读博客 post here,它说在 Storyboard 中你应该使用字符串重载而不是 nav.Configure(Key, ViewController) 并始终在 Storyboard ViewController.
中设置 storyboardId 和 restorationId
Note that because we are using a Storyboard, you must make sure to use
the Configure(string, string) overload, and NOT the Configure(string,
Type) one.
我正在使用 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);
}
}
我肯定漏掉了某个地方的细节!!!
如果你仔细阅读博客 post here,它说在 Storyboard 中你应该使用字符串重载而不是 nav.Configure(Key, ViewController) 并始终在 Storyboard ViewController.
中设置 storyboardId 和 restorationIdNote that because we are using a Storyboard, you must make sure to use the Configure(string, string) overload, and NOT the Configure(string, Type) one.