简体   繁体   English

如何在用户控件库的XAML中访问资源图像?

[英]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.p‌​ng"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在类库中使用用户控件XAML - How to use a User Control XAML within a Class Library 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 如何在FlipView中访问XAML DataTemplate内部的图像控件 - How do I access a image control inside a XAML DataTemplate in a FlipView 如何将Xaml用户控件用作特定类型 - How can I use a xaml user control as a specific type 如何使用XAML和后面的代码序列化WPF用户控件 - How can I serialize wpf user control with xaml and code behind 如何在UWP XAML中为Image控件设置边框 - How can I set border to Image control in UWP XAML 如何访问用户控件中的特定控件? - How do I access a particular control within a user control? 用户在WebView控件中进行交互后,如何隐藏XAML控件 - How do I hide XAML control after a user interacts within a WebView control 如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性? - How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml? 如何将用户控件中选定的 treeView 项目绑定到 WPF XAML 的 MainWindow 中的 tabControl? - How do I bind a selected treeView Item within a user control to a tabControl in the MainWindow in WPF XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM