Images in a WPF Custom Control Library

倾然丶 夕夏残阳落幕 提交于 2019-12-08 07:56:53

问题


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?


回答1:


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




回答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"/>



回答3:


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.



来源:https://stackoverflow.com/questions/963538/images-in-a-wpf-custom-control-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!