动态加载资源字典文件到 wpf 应用程序报错 [英] Dynamically loading resource dictionary files to a wpf application gives an error

查看:26
本文介绍了动态加载资源字典文件到 wpf 应用程序报错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用语句动态添加 xaml 资源文件,

I am trying to add a xaml resource file dynamically using the statement,

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("resources/leaf_styles.xaml", UriKind.Relative) });

这会引发异常,找不到资源resources/leaf_styles.xaml".

我在资源文件夹下的项目中添加了leaf_styles.xaml文件,并且BuildAction设置为Content",CopyAlways设置为True.我仍然收到此错误.有人能帮我指出什么是错的吗??

I added the leaf_styles.xaml file to the project under resource folder and the BuildAction is set to "Content", CopyAlways is set to True. Still I get this error. Could some one help me out pointing whats wrong??

附加信息 -

  • 我不想将 xaml 文件作为资源嵌入
  • 当前项目是.net 3.5类库项目
  • 上面的合并语句写在同一个项目的一个类中
  • 我还手动添加了 [assembly: AssemblyAssociatedContentFile("resources/leaf_styles.xaml")] 一旦我认为这不起作用(用于测试)

如果我给它一个绝对位置,它就可以正常工作.

If I give it as an absolute location, it is working properly.

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"D:foo	runkin
esourcesleaf_styles.xaml", UriKind.Absolute) });

推荐答案

终于成功了.这是我所做的,

At last, it worked. Here is what I did,

  1. 通过'http://msdn.microsoft.com/en-us/library/aa970069.aspx.
  2. 将 Uri 模式更改为

  1. Went thru' http://msdn.microsoft.com/en-us/library/aa970069.aspx.
  2. Changed the Uri pattern to

var foo = new Uri("pack://siteoforigin:,,,/resources/leaf_styles.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });

这篇关于动态加载资源字典文件到 wpf 应用程序报错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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