在代码wpf中访问资源字典

发布于 2024-09-15 08:24:06 字数 768 浏览 7 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

披肩女神 2024-09-22 08:24:06

我使用的是 UriKind,比如

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

HTH

What I use is with UriKind like

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

HTH

愚人国度 2024-09-22 08:24:06

@Prince Ashitaka 答案告诉您如何更正您的 URI

但是,访问 ResourceDictionary 的首选方法是在 XAML 中将其添加为合并字典

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ImageResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

,然后您可以使用 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

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ImageResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

then you can access it via code using the TryFindResource(string Key) from any code behind file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文