WPF图标和Uri

Ste*_*ila 1 vb.net wpf resources icons

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

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}
Run Code Online (Sandbox Code Playgroud)

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

<ResourceDictionary Source="pack://application:,,,/ELE100WalkerWPF;component/Resources/Colors.xaml" />
Run Code Online (Sandbox Code Playgroud)

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

<ResourceDictionary Source="Resources/Colors.xaml" />
Run Code Online (Sandbox Code Playgroud)

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

Cle*_*ens 5

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

你也不能在UriSource没有调用BeginInit和设置的情况下设置BitmapImage的属性EndInit.使用带有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)
Run Code Online (Sandbox Code Playgroud)

如果图像资源位于引用的程序集(称为ELE100WalkerWPF)中,则必须包含程序集名称,如下所示:

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