在 UWP 应用中合并 ResourceDictionary 初始化 [英] Merged ResourceDictionary initalization in UWP app

查看:23
本文介绍了在 UWP 应用中合并 ResourceDictionary 初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UWP 应用开发过程中,我注意到了一些我很难解释的奇怪之处.

During development of my UWP app I have noticed and intersting oddity which I have hard time explaining.

我使用 MvvmLight,我决定在单独的 ResourceDictionary Core.xaml 中添加 ViewModelLocator 资源实例,该实例将从 MergedDictionaries 中引用 App.xaml.以下是App.xaml的内容:

I user MvvmLight and I decided to add the ViewModelLocator resource instance in a separate ResourceDictionary Core.xaml which will be referenced from MergedDictionaries in App.xaml. Following is the content of App.xaml:

<Application ...>
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Core.xaml" />
            <ResourceDictionary Source="Resources/Converters.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
</Application>

Core.xaml 的内容:

<ResourceDictionary ...>
    <viewModel:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>

现在我假设 Core.xaml 中的资源是在 App.xaml.cs 中的 InitializeComponent 方法调用期间初始化的,但是当我尝试使用 ServiceLocator 类(在 MvvmLight 中 ViewModelLocator 的构造函数中设置) - 像这样 - ServiceLocator.Current.GetInstance().Navigate(); - 我收到一条异常消息:

Now I supposed the resources in Core.xaml are initialized during the InitializeComponent method call in App.xaml.cs, but when I tried to use the ServiceLocator class (which is set in the constructor of ViewModelLocator in MvvmLight) - like this - ServiceLocator.Current.GetInstance<INavigationService>().Navigate<MainViewModel>(); - I get an exception saying:

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的构造函数中设置断点,在Window被激活之前是不会被调用的.更有趣的是 - 如果我手动引用 Locator 资源键(例如将 Debug.WriteLine(Resources["Locator"]); 放在 ServiceLocator 的调用之上),一切正常.如果我将 ViewModelLocator 资源直接移动到 App.xaml,情况也是如此 - 然后它在 IntializeComponent 期间被实例化.

Indeed, if I put an breakpoint in the constructor of ViewModelLocator, it is not called before the Window is activated. More interestingly still - if I manually reference the Locator resource key (for example putting Debug.WriteLine(Resources["Locator"]); above the call of ServiceLocator), everything works fine. The same goes if I move the ViewModelLocator resource directly to App.xaml - then it is instantiated during IntializeComponent.

UWP 应用程序中是否存在合并资源字典的延迟实例化?或者为什么它会这样?

Is there a lazy instantiation of merged resource dictionaries in UWP apps? Or why does it behave this way?

推荐答案

UWP 中的一个 ResourceDictionary 没有任何隐藏的代码(没有 InitializeComponent).因此,ResourceDictionary 中定义的任何类引用都不会被直接初始化.

A ResourceDictionary in UWP doesn't have any code behind (no InitializeComponent). Therefore, any class references defined in a ResourceDictionary won't be initialized directly.

App.InitializeComponent 也不会为您执行此操作.UWP 中的资源词典不提供此功能 - 不要问我为什么.

Neither does the App.InitializeComponent do this for you. Resource dictionaries in UWP just don't provide this functionallity - don't ask me why.

您可以通过尝试初始化 ResourceDictionary 中的 DataTemplate 来轻松尝试此操作.
这应该 - 遗憾的是 - 都不起作用.

You can easily try this by trying to initialize a DataTemplate in a ResourceDictionary.
This should - sadly - neither work.

但是,在后面的代码中使用 Resources["Locator"] 访问会触发类的构造函数,你就没事了.

However, using the Resources["Locator"] access in code behind triggers the constructor of the class and you're fine.

这不是解决方案,而是对您的问题的解释.希望能帮到你.

This ain't be a solution, but a explanation of your problem. I hope it helps you.

这篇关于在 UWP 应用中合并 ResourceDictionary 初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆