在代码 wpf 中访问资源字典

编程入门 行业动态 更新时间:2024-10-24 06:30:09
本文介绍了在代码 wpf 中访问资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

同一程序集中的同一行代码适用于一个测试夹具,但不适用于另一个.这是代码行:

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") };

我在另一个测试装置中得到的错误是 System.UriFormatException : Invalid URI: Invalid port specified.

The error I get in the other test fixture is System.UriFormatException : Invalid URI: Invalid port specified.

uri 字符串也适用于 xaml.有没有更好的方法在代码中加载资源字典?

The uri string also works in xaml. Is there a better way to load a resource dictionary in code?

干杯,贝瑞尔

=== 更新 ===

正如我在这篇帖子中发现的那样,发生无效端口,因为包方案未注册,可以使用如下代码完成:

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);

我猜测,能够无误地加载带有包方案的字典的测试装置是因为 SUT 是那里的用户控件,并且在创建用户控件的实例时以某种方式加载资源.

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.

推荐答案

我使用的是 UriKind like

What I use is with UriKind like

var resource = new ResourceDictionary { Source = new Uri("/myAssemblyName;component/Themes/generic.xaml", UriKind.RelativeOrAbsolute) };

HTH

更多推荐

在代码 wpf 中访问资源字典

本文发布于:2023-11-12 03:44:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   代码   资源   wpf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!