在开发我的 UWP 应用程序期间,我注意到一些有趣的奇怪现象,我很难解释。
我使用 MvvmLight 并决定在单独的 ResourceDictionary 中添加 ViewModelLocator 资源实例核心.xaml这将被引用自MergedDictionaries
in App.xaml。
以下是内容App.xaml:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Core.xaml" />
<ResourceDictionary Source="Resources/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
内容核心.xaml:
<ResourceDictionary ...>
<viewModel:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>
现在我认为资源在核心.xaml期间被初始化InitializeComponent
方法调用应用程序.xaml.cs,但是当我尝试使用ServiceLocator
类(在 MvvmLight 中 ViewModelLocator 的构造函数中设置) - 像这样 -ServiceLocator.Current.GetInstance<INavigationService>().Navigate<MainViewModel>();
- 我收到一个例外:
An exception of type 'System.InvalidOperationException' occurred in
Microsoft.Practices.ServiceLocation.dll but was not handled in user code
Additional information: ServiceLocationProvider must be set.
事实上,如果我在 ViewModelLocator 的构造函数中放置一个断点,则在激活窗口之前不会调用它。更有趣的是 - 如果我手动引用Locator资源键(例如放置Debug.WriteLine(Resources["Locator"]);
高于ServiceLocator
),一切正常。如果我移动,情况也是如此ViewModelLocator
资源直接到 App.xaml - 然后在期间实例化IntializeComponent
.
UWP 应用程序中是否存在合并资源字典的延迟实例化?或者为什么它会这样?