动态加载资源字典文件到wpf应用程序会出现错误
我正在尝试使用以下语句动态添加 xaml 资源文件,
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。我仍然收到这个错误。有人可以帮我指出哪里出了问题吗?
附加信息 -
- 我不想将xaml文件作为资源嵌入
- 当前项目是.net 3.5类库项目
- 上面的mergedictionary语句是这样写的在属于同一项目的类中
- 一旦我发现这不起作用(对于测试)
更新
如果我将其指定为绝对位置,则它可以正常工作。
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"D:\foo\trunk\bin\resources\leaf_styles.xaml", UriKind.Absolute) });
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) });
This is throwing an exception, Cannot locate resource 'resources/leaf_styles.xaml'.
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??
Additional information -
- I don't want to embed the xaml file as a resource
- The current project is a .net 3.5 class library project
- The above mergedictionary statement is written in a class belonging to the same project
- I also added the [assembly: AssemblyAssociatedContentFile("resources/leaf_styles.xaml")] manually once I figured that this is not working (for testing)
Update
If I give it as an absolute location, it is working properly.
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"D:\foo\trunk\bin\resources\leaf_styles.xaml", UriKind.Absolute) });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后,它成功了。这就是我所做的,
将 Uri 模式更改为
At last, it worked. Here is what I did,
Changed the Uri pattern to
要加载内容文件,您可以调用 GetContentStream 方法应用程序类,传递标识所需内容文件的包 URI。
查看
http://msdn.microsoft.com/en-us/library/aa970494.aspx#Content_Files
编辑
我这样做成功了
To load a content file, you can call the GetContentStream method of the Application class, passing a pack URI that identifies the desired content file.
Checkout
http://msdn.microsoft.com/en-us/library/aa970494.aspx#Content_Files
EDIT
I did it successfully like this
我遇到了同样的“缺少资源问题”并挠了我的头几个小时。然后我意识到我的程序集名称包含点(.)并更改了资源程序集名称,再次测试并且它有效。这是我想要加载的 16x16 png 图像文件。但我发现点式程序集名称会导致 soma 情况出现错误,而不会导致其他情况出现错误。
我对这两种情况使用了相同的代码,但结果不同。不知道是不是wpf的bug。
I encountered same "missing resource problem" and scratched my head for hours. Then I realized that my assembly name contains dots (.) and changed the resource assembly name, tested again and it worked. It was a 16x16 png image file which ı wanted to load. But I see that dotted assembly names causes error for soma cases and does not cause error for other cases.
I used the same code for both cases but results are different. I don't know if it is a wpf bug.