WPF 图标和 Uri

WPF Icon and Uri

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

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" />

谁能解释一下为什么图像没有显示,我该如何解决这个问题?提前致谢

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

您也可以在不调用 BeginInitEndInit 的情况下设置 BitmapImage 的 UriSource 属性。使用带 Uri 参数的 BitmapImage 构造函数。

假设 ShowMCF.png 位于您的 Visual Studio 项目的顶级文件夹中,并且其构建操作设置为 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)