在代码wpf中访问资源字典
同一程序集中的同一行代码适用于一个测试夹具,但不适用于另一测试夹具。这是代码行:
var dic = new ResourceDictionary { Source = new Uri("pack://application:,,,/MyApp.Wpf;component/ImageResources.xaml") };
我在另一个测试装置中遇到的错误是 System.UriFormatException :无效的 URI:指定的端口无效。
uri 字符串也适用于 xaml。有没有更好的方法在代码中加载资源字典?
干杯,
Berryl
=== 更新 ===
正如我在 这篇文章中发现的< /a>,发生了无效端口,因为包方案未注册,这可以使用如下代码来完成:
if (!UriParser.IsKnownScheme("pack"))
UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);
我猜测能够使用包方案加载字典且没有错误的测试装置是因为SUT 是其中的一个用户控件,并且在创建用户控件的实例时以某种方式加载资源。
The same line of code in the same assembly works for one test fixture but not another. Here is the line of code:
var dic = new ResourceDictionary { Source = new Uri("pack://application:,,,/MyApp.Wpf;component/ImageResources.xaml") };
The error I get in the other test fixture is System.UriFormatException : Invalid URI: Invalid port specified.
The uri string also works in xaml. Is there a better way to load a resource dictionary in code?
Cheers,
Berryl
=== UPDATE ===
As I found in this posting, an Invalid port was occurring because the pack scheme wasn't registered, which can be done with code like so:
if (!UriParser.IsKnownScheme("pack"))
UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);
I am guessing that the test fixture that was able to load the dictionary with the pack scheme without error is because the SUT is a user control there, and is somehow loading resources when an instance of the user control is created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用的是 UriKind,比如
HTH
What I use is with UriKind like
HTH
@Prince Ashitaka 答案告诉您如何更正您的 URI
但是,访问 ResourceDictionary 的首选方法是在 XAML 中将其添加为合并字典
,然后您可以使用
TryFindResource(string Key)
TryFindResource(string Key) 通过代码访问它code> 来自任何代码隐藏文件@Prince Ashitaka answer tells you how to correct your URI
However the preferred way of accessing a ResourceDictionary is that in XAML you add it on as a merged dictionary
then you can access it via code using the
TryFindResource(string Key)
from any code behind file