我需要将图像放在自定义控件的默认视图中。但是,每当我尝试测试控件时,它都无法找到图像。我试图将它编译为嵌入式资源,并且只是 VS 中的普通资源。这些都没有奏效。那么有没有正确的方法来做到这一点?
2 回答
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 :
<Image Source="pack://application:,,,/Images/MyImage.png"/>
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.
It's available on NuGet:
Install-Package FamFamFam.Flags.Wpf
The source is up on GitHub:
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.