WPF - 使用pack URI从转换器返回图像

时间:2011-08-19 09:33:17

标签: c# wpf uri ivalueconverter

我想创建一个WPF转换器,根据布尔值返回某个图像。

我到目前为止得到了以下代码:

 return (bool) value
                   ? new BitmapImage(new Uri("pack://application:,,,MyApp.ApplicationResources;component/Resources/image1.png", UriKind.Absolute))
                   : new BitmapImage(new Uri("pack://application:,,,MyApp.ApplicationResources;component/Resources/image2.png", UriKind.Absolute));

然而,这给了我例外

System.Windows.Markup.XamlParseException occurred 
Message=The URI prefix is not recognized.

图像位于文件夹“Resources”中的MyApp.ApplicationResources的引用程序集中,并且都设置为Content Type:Resource。

如果我在xaml中的图像的source属性中使用相同的URI,它可以正常工作!

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:3)

您的/似乎错过了Uri

return (bool) value
               ? new BitmapImage(new Uri("pack://application:,,,/MyApp.ApplicationResources;component/Resources/image1.png", UriKind.Absolute))
               : new BitmapImage(new Uri("pack://application:,,,/MyApp.ApplicationResources;component/Resources/image2.png", UriKind.Absolute));