问题
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.png"
来源:https://stackoverflow.com/questions/21167561/how-can-i-access-a-resource-image-within-xaml-in-a-user-control-library