How can I access a resource image within XAML in a user control library?

两盒软妹~` 提交于 2019-12-11 12:22:58

问题


I'm writing a library of WPF user controls and am having trouble with a resource image that I'm trying to access via some XAML. Just for fun, the image displays as expected at design time and only fails at run time.

I've tried setting the build action to "none", "content", "resource" and "embedded resource", but I keep getting the following cryptic exception:

'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '18' and line position '6'.

Looking at the inner exception, it seems to be a problem with the pack URI. When I try:

<ImageBrush x:Key="mybrush" ImageSource="pack://application:,,,/Resources/an image.png" />

I get:

Assembly.GetEntryAssembly() returns null.

OK, so this is probably because my library is now being called from within another assembly. But when I follow the recommendations and try:

<ImageBrush x:Key="mybrush" ImageSource="pack://application:,,,my_assembly;component;/Resources/an image.png" />

I get an inner exception of:

"The URI prefix is not recognized."

To add to the fun, I have another image resource which I have no problem accessing via actual C# code (i.e. not through XAML).

What am I doing wrong? Feel like it will be really simple, but am head-desking at the moment.


回答1:


Change ImageSource to

ImageSource="pack://application:,,,/my_assembly;component/Resources/your_image.p‌​ng"


来源:https://stackoverflow.com/questions/21167561/how-can-i-access-a-resource-image-within-xaml-in-a-user-control-library

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