[英]How can I access a resource image within XAML in a user control library?
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. 我正在编写WPF用户控件的库,并且遇到了试图通过某些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.' '在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供值引发了异常。 Line number '18' and line position '6'. 行号“ 18”和行位置“ 6”。
Looking at the inner exception, it seems to be a problem with the pack URI. 从内部异常来看,pack URI似乎是一个问题。 When I try: 当我尝试:
<ImageBrush x:Key="mybrush" ImageSource="pack://application:,,,/Resources/an image.png" />
I get: 我得到:
Assembly.GetEntryAssembly() returns null. Assembly.GetEntryAssembly()返回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." “无法识别URI前缀。”
To add to the fun, I have another image resource which I have no problem accessing via actual C# code (ie not through XAML). 为了增加乐趣,我还有另一个图像资源,可以通过实际的C#代码(即,不通过XAML)访问该图像资源。
What am I doing wrong? 我究竟做错了什么? Feel like it will be really simple, but am head-desking at the moment. 感觉这真的很简单,但目前是总公司。
将ImageSource更改为
ImageSource="pack://application:,,,/my_assembly;component/Resources/your_image.png"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.