UWP/MVVMlight:替换过时的ServiceLocator和SimpleIoc寄存器 [英] UWP / MVVMlight : Replacing Obsolete ServiceLocator and SimpleIoc Register
问题描述
我再次打开这个话题.
更新UWP应用时,我面临一个挑战: 我将提供更多详细信息:这是我的旧代码,更新之前可以在UWP/Win10 10240上完美运行 在我的app.xaml中
I face a probelm when I update my UWP app: I'm gonna give more detail: Here is my old code before update running perfectly on UWP/Win10 10240 in my app.xaml
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>
在我的App.xaml.cs
in my App.xaml.cs
public class ViewModelLocator
{
public const string MainMenuPageKey = "MainMenuPage";
public const string QuestionPageKey = "QuestionPage";
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
var nav = new NavigationService();
nav.Configure(MainMenuPageKey, typeof(MainMenuPage));
nav.Configure(QuestionPageKey, typeof(QuestionPage));
SimpleIoc.Default.Register<INavigationService>(() => nav);
SimpleIoc.Default.Register<IDialogService, DialogService>();
SimpleIoc.Default.Register<IDataService, DataService>();
SimpleIoc.Default.Register<MainMenuViewModel>();
}
public MainMenuViewModel MainMenu=>
ServiceLocator.Current.GetInstance<MainMenuViewModel>();
在我的vm:ViewModelLocator.cs中:
in my vm:ViewModelLocator.cs:
随着servicelocator的消失,我想更新到UWP/Win10 16299,这是我使用的代码:
I want to update to UWP/Win10 16299 as servicelocator has disappear, here is the code I use:
public const string MainMenuPageKey = "MainMenuPage";
public const string QuestionPageKey = "QuestionPage";
static ViewModelLocator()
{
var nav = new NavigationService();
nav.Configure(MainMenuPageKey, typeof(MainMenuPage));
nav.Configure(QuestionPageKey, typeof(QuestionPage));
SimpleIoc.Default.Register<INavigationService>();
SimpleIoc.Default.Register<IDialogService, DialogService>();
SimpleIoc.Default.Register<IDataService, DataService>();
SimpleIoc.Default.Register<MainMenuViewModel>();
}
public MainMenuViewModel MainMenu =>
SimpleIoc.Default.GetInstance<MainMenuViewModel>();
看起来像注册"会导致崩溃
it looks like 'register' creates the crash
您有解决这个问题的诀窍吗?
Do you have any trick to solve this?
致谢;
推荐答案
感谢您的帮助.
实际上是为了解决该问题,我在app.xaml.cs中动态创建了ViewModelLocator的实例.然后,在实例上放置一个断点.由于我在nuget中下载了ServiceLocator库,系统崩溃了.
Actually to solve it, I create dynamically an instance of the ViewModelLocator in my app.xaml.cs. Then I put a breakpoint on the instance. The system crashed because of the ServiceLocator library I've downloaded in nuget.
删除此Dll后,一切运行正常.
Once this Dll is deleted, everything runs fine.
这篇关于UWP/MVVMlight:替换过时的ServiceLocator和SimpleIoc寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!