【问题标题】:WPF Icon and UriWPF 图标和 Uri
【发布时间】:2018-10-08 10:09:57
【问题描述】:

我正在尝试将项目中的图标直接加载到主目录中。 为了做到这一点,我这样做:

Dim uriSrc As Uri = New Uri("pack://ELE100WalkerWPF:,,,/assemblyName;Component/ShowMCF.png")
Dim bitmapImage As BitmapImage = New BitmapImage With {.UriSource = uriSrc}
Dim image As System.Windows.Controls.Image = New System.Windows.Controls.Image() With {.Source = bitmapImage}

即使对于我的样式我以相同的方式引用它们,这也根本不起作用:

<ResourceDictionary Source="pack://application:,,,/ELE100WalkerWPF;component/Resources/Colors.xaml" />

唯一的区别是我的样式是在 application.xaml 的 MergeDictionary 中定义的,像这样

<ResourceDictionary Source="Resources/Colors.xaml" />

有人可以解释为什么图像没有显示以及我该如何解决这个问题?提前致谢

【问题讨论】:

    标签: wpf vb.net resources icons


    【解决方案1】:

    pack://ELE100WalkerWPF:... 不是有效的Pack URI

    如果不调用BeginInitEndInit,您也不能设置BitmapImage 的UriSource 属性。使用带有 Uri 参数的 BitmapImage 构造函数。

    假设 ShowMCF.png 位于 Visual Studio 项目的顶级文件夹中,并且其 Build Action 设置为 Resource,则应该可以:

    Dim uri As Uri = New Uri("pack://application:,,,/ShowMCF.png")
    Dim bitmapImage As BitmapImage = New BitmapImage(uri)
    

    如果图像资源在引用的程序集(称为 ELE100WalkerWPF)中,则必须像这样包含程序集名称:

    Dim uri As Uri = New Uri("pack://application:,,,/ELE100WalkerWPF;component/ShowMCF.png")
    Dim bitmapImage As BitmapImage = New BitmapImage(uri)
    

    【讨论】:

    • 克莱门斯你成功了!!
    • 好的,由于 BitmapImage 的初始化,它起作用了,但是为了使它起作用,我必须像这样设置 URI:pack://application:,,,/ELE100WalkerWPF;component/ShowMCF.png :)
    • @StefanoAquila 这表明图像资源位于不同的程序集中,您在问题中没有提及。如果它在同一个程序集中,您可以省略 ELE100WalkerWPF;component 部分。
    • 我也没有。URI 的问题可能是因为我有一个包含我的项目的外部应用程序吗?
    • 那是一个不同的程序集。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2021-02-25
    • 2011-08-01
    • 2016-05-14
    • 2011-03-02
    相关资源
    最近更新 更多