【问题标题】:WPF - Return image from converter using pack URIWPF - 使用包 URI 从转换器返回图像
【发布时间】:2011-08-19 09:33:16
【问题描述】:

我想创建一个 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.

图像位于文件夹“资源”中的 MyApp.ApplicationResources 的引用程序集中,并且两者都设置为内容类型:资源。

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

有什么想法吗? 谢谢

【问题讨论】:

  • 您尝试过不使用 UriKind.Absolute 吗?我使用完全相同的语法,而且效果很好。

标签: c# wpf uri ivalueconverter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    相关资源
    最近更新 更多