WPF 图标和 Uri

问题描述 投票:0回答:1

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

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个回答
6
投票

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

您也可以不调用

UriSource
BeginInit
来设置 BitmapImage 的
EndInit
属性。使用带有 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)
© www.soinside.com 2019 - 2024. All rights reserved.