当我向其添加第二个对象时,未加载UWP ResourceDictionary [英] UWP ResourceDictionary is not being loaded when I add a second object to it
问题描述
我有一个适用于Windows 10的UWP应用程序,其中插入了MVVM Light. 我将ViewModelLocator存储在App.Resources中. 当我的App.Resources中只有ViewModelLocator时,一切正常.
I have a UWP application for Windows 10 with MVVM Light plugged in. I store a ViewModelLocator in the App.Resources. When I have just a ViewModelLocator in my App.Resources, everything works fine.
<Application.Resources>
<viewModel:ViewModelLocator x:Key="Locator" />
</Application.Resources>
一旦我添加了String,Converter或类似的东西,应用程序就不会崩溃,但是ViewModelLocator构造函数不再被调用. 没有引发任何错误或异常,只是没有加载ResourceDictionary或在加载过程中失败.
As soon as I add a String, Converter or something similar, application doesn't crash but the ViewModelLocator constructor is not being called anymore. No errors or exceptions being thrown, just ResourceDictionary is not being loaded or fails during load.
<Application.Resources>
<viewModel:ViewModelLocator x:Key="Locator" />
<x:String x:Key="SampleString">Hello</x:String>
</Application.Resources>
如果我添加样式,数据模板,画笔,颜色-一切正常.
If I add a Style, DataTemplate, Brush, Color - everything works fine.
以前没有注意到Windows Phone 8,Silverlignt或WPF上的行为. 将样式或对象移动到单独的ResourceDictionaries并使用MergedDictionaries加载它们并没有帮助.
Haven't noticed that behavior on Windows Phone 8, Silverlignt or WPF before. Moving styles or objects to separate ResourceDictionaries and load them using MergedDictionaries didn't help.
我想在ResourceDictionary中有一个对象列表,以便在应用程序启动时自动调用这些对象的所有构造函数. 请告知.
I would like to have a list of objects in ResourceDictionary so that all constructors of these objects are being called automatically on the app start. Please advise.
PS: 即使是两个类似的转换器也无法工作,而一个转换器的创建没有任何问题
P.S.: Even two similar converters don't work, while one is created without any problem
<Application.Resources>
<!--<viewModel:ViewModelLocator x:Key="Locator" />-->
<converters:StringFormatConverter x:Key="StringFormat1" />
<converters:StringFormatConverter x:Key="StringFormat2" />
</Application.Resources>
寻找一个ResourceDictionary用法的示例,发现了类似的问题: 在UWP应用中合并ResourceDictionary初始化
Looking for an example of ResourceDictionary usage, found similar question: Merged ResourceDictionary initalization in UWP app
推荐答案
这都是因为延迟初始化.我做了一些实验,弄清楚了这张照片.希望您能理解. http://screencast.com/t/mxyBGBDuZ
That's all because of lazy initialization. I've made some experiments and figured out this picture. I hope you will catch the idea. http://screencast.com/t/mxyBGBDuZ
这篇关于当我向其添加第二个对象时,未加载UWP ResourceDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!