这个URI有什么问题?

tom*_*jen 6 c# wpf

HEJ

我正在尝试使用Uri在wpf应用程序中加载(嵌入)图像,但我不断获得异常.

代码是:

new BitmapImage(new Uri("pack://application:,,,,/Icons/m.png"));
Run Code Online (Sandbox Code Playgroud)

(如果不清楚,我正在尝试从Icons文件夹加载m.png文件,该文件夹已被标记为嵌入式资源).

而例外是

NotSupportetException (the URI prefix is not recognized)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我uri应该是什么?

Dar*_*rov 2

您可以看看这篇博文。解决方案是注册一个自定义 uri 解析器,以便它识别pack协议:

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