如何在用户控件库中访问XAML中的资源图像? [英] How can I access a resource image within XAML in a user control library?

查看:238
本文介绍了如何在用户控件库中访问XAML中的资源图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个WPF用户控件库,但我正在尝试通过某些XAML访问资源映像。只是为了好玩,图像在设计时按预期显示,并且仅在运行时失败。

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:


'提供
'的价值System.Windows.Baml2006.TypeConverterMarkupExtension'抛出
异常。'行号'18'和行位置'6'。

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

查看内部异常,它似乎是包URI的问题。当我尝试:

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

我得到:


Assembly.GetEntryAssembly()返回null。

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

我得到一个内部例外:


无法识别URI前缀。

"The URI prefix is not recognized."

为了增加乐趣,我有另一张图片通过实际的C#代码访问我没有问题的资源(即不通过XAML)。

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.

推荐答案

将ImageSource更改为

Change ImageSource to

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

这篇关于如何在用户控件库中访问XAML中的资源图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆