如何在运行时加载 MergedResources?我的代码没有按预期工作
我有一个桌面 WPF 应用程序,可以从网络加载其资源,这使我们能够执行不同的操作,例如...如果需要,可以在假期前后添加圣诞节主题。要加载主题,我只需将应用程序当前资源字典替换为我们自己的资源字典,如下所示。
ResourceDictionary resources = null;
System.Net.WebClient client = new System.Net.WebClient();
using (Stream s = client.OpenRead("http://www.mywebsite.com/MyXAMLFile.xaml"))
{
try
{
resources = (ResourceDictionary)XamlReader.Load(s);
}
catch
{ }
}
Application.Current.Resources = resources;
好吧,我们已经到了这样的地步:我想将一些资源分成不同的文件,而不是只使用一个包含所有内容的大资源文件。我认为仅使用合并的资源这会相当简单。我以为对上面的代码进行简单的修改就能使其工作,但事实证明并非如此。这就是我所期望的我必须要做的事情。
//replace the last line
Application.Current.Resources = resources;
//with this line
Application.Current.Resources.MergedDictionaries.Add(resources);
当应用程序运行时,它只使用普通样式,并且不考虑我添加的合并资源。我在这里做错了什么?我如何能够在运行时动态添加合并的资源(和多个资源)并让它们正常工作?
谢谢, 凯尔
I have a desktop WPF application that loads it's resources from the web, this allows us to do different things like...add a christmas theme later on down the road around the holidays if we want. To load the Theme, I would just replace the Applications current resource dictionary with our own resource dictionary like so.
ResourceDictionary resources = null;
System.Net.WebClient client = new System.Net.WebClient();
using (Stream s = client.OpenRead("http://www.mywebsite.com/MyXAMLFile.xaml"))
{
try
{
resources = (ResourceDictionary)XamlReader.Load(s);
}
catch
{ }
}
Application.Current.Resources = resources;
Well we got to the point where I wanted to kinda seperate some of the resources into different files instead of using just ONE big resource file that contained everything. I thought this would be rather simple just using merged resources. I thought a simple modification to the code above would make it work, but it turns out it doesn't. This is what I expected I would have to do.
//replace the last line
Application.Current.Resources = resources;
//with this line
Application.Current.Resources.MergedDictionaries.Add(resources);
When the application is run, it just uses the normal styles, and doesn't take into account the merged resources I added. What am I doing wrong here? How am I able to dynamically add merged resources (and multiple ones) at runtime and get them to work properly?
Thanks,
Kyle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很抱歉,显然它确实有效。使用前一种方法时,我已经在 App.xaml 中定义了默认样式,并且正在使用 StaticResources,当我更改为使用合并字典时,我必须使用 DynamicResources。我以为我删除了 App.xaml 中的样式,但由于我的 Visual Studio 解决方案中有多个项目,所以我删除了错误的 App.xaml 文件中的样式!
该死的 Visual Studio 使得如果同一解决方案中有多个项目,就无法判断您正在编辑哪个 App.xaml 文件:-)。
再次抱歉!如果我知道如何删除问题我会的。
My apologies, apparently it does work. With the former method I had the default styles defined already in the App.xaml, and was using StaticResources, when I changed to use the merged dictionaries I had to use DynamicResources. I thought I wiped out the styles in the App.xaml, but since there was multiple projects in my Visual Studio solution, I deleted the styles in the wrong App.xaml file!
Damn Visual Studio for making it impossible to tell what App.xaml file you're editing if there's multiple projects in the same solution :-).
Sorry again! If I knew how to delete a question I would.