gpt4 book ai didi

WPF 图标和 Uri

转载 作者:行者123 更新时间:2023-12-02 00:28:50 24 4
gpt4 key购买 nike

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

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)

关于WPF 图标和 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52699959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com