访问代码wpf中的资源字典

Ber*_*ryl 16 wpf uri resourcedictionary

同一组件中的相同代码行适用于一个测试夹具,但不适用于另一个.这是代码行:

var dic = new ResourceDictionary { Source = new Uri("pack://application:,,,/MyApp.Wpf;component/ImageResources.xaml") };
Run Code Online (Sandbox Code Playgroud)

我在另一个测试夹具中得到的错误是System.UriFormatException:无效的URI:指定的端口无效.

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

干杯,
Berryl

===更新===

正如我在这篇文章中发现的那样,由于没有注册包方案,因此发生了无效端口,这可以通过以下代码完成:

if (!UriParser.IsKnownScheme("pack"))
     UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);
Run Code Online (Sandbox Code Playgroud)

我猜测能够使用pack方案加载字典而没有错误的测试夹具是因为SUT是用户控件,并且在创建用户控件的实例时以某种方式加载资源.

Pri*_*aka 27

我用的是和UriKind一样的

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

HTH