WPF自定义控件库中的图像

时间:2022-03-04 13:20:52

I need to put an image in the default view of a custom control. However, whenever I try to test the control it can't locate the image. I have tried to compile it as an embedded resource and just a plain resource in VS. Neither of these have worked. So is there a correct way to do this?

我需要将图像放在自定义控件的默认视图中。但是,每当我尝试测试控件时,它都无法定位图像。我试图将其编译为嵌入式资源,而只是VS中的普通资源。这些都没有奏效。那么有正确的方法吗?

3 个解决方案

#1


2  

Look at this post: How can I get a BitmapImage from a Resource?

看看这篇文章:如何从资源中获取BitmapImage?

#2


4  

That's probably because you specified the image path as a relative path. You should use the Pack URI Scheme to specify that the resource is in the current assembly. For instance :

这可能是因为您将图像路径指定为相对路径。您应该使用包URI方案来指定资源在当前程序集中。例如 :

<Image Source="pack://application:,,,/Images/MyImage.png"/>

#3


1  

I have an open-source library that allows you to include country flags in your WPF application via a value converter. The flags images are stored as resources within the assembly.

我有一个开源库,允许您通过值转换器在WPF应用程序中包含国家标志。标志图像作为资源存储在程序集中。

WPF自定义控件库中的图像

It's available on NuGet:

它可以在NuGet上找到:

Install-Package FamFamFam.Flags.Wpf

安装包FamFamFam.Flags.Wpf

The source is up on GitHub:

源代码在GitHub上:

https://github.com/drewnoakes/famfamfam-flags-wpf

https://github.com/drewnoakes/famfamfam-flags-wpf

You can take a look to see how the images are embedded and the Pack URI scheme is used.

您可以查看如何嵌入图像并使用Pack URI方案。

#1


2  

Look at this post: How can I get a BitmapImage from a Resource?

看看这篇文章:如何从资源中获取BitmapImage?

#2


4  

That's probably because you specified the image path as a relative path. You should use the Pack URI Scheme to specify that the resource is in the current assembly. For instance :

这可能是因为您将图像路径指定为相对路径。您应该使用包URI方案来指定资源在当前程序集中。例如 :

<Image Source="pack://application:,,,/Images/MyImage.png"/>

#3


1  

I have an open-source library that allows you to include country flags in your WPF application via a value converter. The flags images are stored as resources within the assembly.

我有一个开源库,允许您通过值转换器在WPF应用程序中包含国家标志。标志图像作为资源存储在程序集中。

WPF自定义控件库中的图像

It's available on NuGet:

它可以在NuGet上找到:

Install-Package FamFamFam.Flags.Wpf

安装包FamFamFam.Flags.Wpf

The source is up on GitHub:

源代码在GitHub上:

https://github.com/drewnoakes/famfamfam-flags-wpf

https://github.com/drewnoakes/famfamfam-flags-wpf

You can take a look to see how the images are embedded and the Pack URI scheme is used.

您可以查看如何嵌入图像并使用Pack URI方案。