C# 从 WPF UserControl 访问 ResourceDictionary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/940879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing ResourceDictionary from WPF UserControl
提问by
I'm trying to access a resource dictionary in a UserControl code-behind via C# and I'm having little success.
我正在尝试通过 C# 访问 UserControl 代码隐藏中的资源字典,但收效甚微。
Merged Dictionary:
合并字典:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/BiometricDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Embedded Dictionary:
嵌入式字典:
<UserControl.Resources>
<BitmapImage x:Key="imageDefault">/Resources/Images/default_thumb.png</BitmapImage>
<BitmapImage x:Key="imageDisconnected">/Resources/Images/disconnect_thumb.png</BitmapImage>
<BitmapImage x:Key="imageFailed">/Resources/Images/failed_thumb.png</BitmapImage>
<BitmapImage x:Key="imageSuccess">/Resources/Images/success_thumb.png</BitmapImage>
</UserControl.Resources>
Code behind:
后面的代码:
var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("/Resources/BiometricDictionary.xaml", UriKind.Relative);
I've tried all of the examples and helpful tips but coming up short. Right now, success would be the ability to load the dictionary. Any suggestions?
我已经尝试了所有的例子和有用的提示,但还是很短。现在,成功将是加载字典的能力。有什么建议?
回答by Mark Carpenter
So, you have a ResourceDictionary defined in a UserControl's assembly, and would like to access it from that UserControl's code-behind?
那么,您在 UserControl 的程序集中定义了一个 ResourceDictionary,并且想从该 UserControl 的代码隐藏中访问它?
You should be able to. However, if the code you listed is in the constructor, you may not have access to the resource dictionary (might not be loaded yet). Try adding that same code to your UserControl's "loaded" event, and see if that works. If you're simply trying to access a resource, such as a style or template, using the "FindResource" or "TryFindResource" functions directly from your class should work as well (i.e. you don't need to have an object of type "ResourceDictionary").
你应该能够。但是,如果您列出的代码在构造函数中,您可能无法访问资源字典(可能尚未加载)。尝试将相同的代码添加到 UserControl 的“加载”事件中,看看是否有效。如果您只是尝试访问资源,例如样式或模板,则直接从您的类中使用“ FindResource”或“ TryFindResource”函数也应该可以正常工作(即您不需要具有类型为“的对象”资源字典”)。
Hope that helps!
希望有帮助!
回答by PeterAllenWebb
To access one of your UserControl's XAML resources in your codebehind, all you need to do is access the Resources property of the UserControl. Something like this:
要在代码隐藏中访问 UserControl 的 XAML 资源之一,您需要做的就是访问 UserControl 的 Resources 属性。像这样的东西:
BitmapImage myImage = (BitmapImage)this.Resources["imageDefault"];
Though, the preferred method is to use FindResource(), which will search the entire logical tree for a match to the key, rather than just the object it is called on.
不过,首选方法是使用 FindResource(),它将搜索整个逻辑树以查找与键的匹配项,而不仅仅是调用它的对象。
BitmapImage myImage = (BitmapImage)this.FindResource("imageDefault");
回答by PeterAllenWebb
d'Oh...after compiling to the local bin so that references are relative, I implemented the pack URI solution found here: ResourceDictionary in a separate assemblyand then FindResource(x:key value here).
d'Oh...在编译到本地 bin 以便引用是相对的之后,我实现了在这里找到的包 URI 解决方案:ResourceDictionary 在一个单独的程序集中,然后 FindResource(x:key value here)。
@PeterAllenWeb, @Pwninstein, thanks for your quick responses and getting me thinking again.
@PeterAllenWeb、@Pwninstein,感谢您的快速回复并让我重新思考。
回答by Max Mazur
Try to remove the forward slash infront of your location. The only time you should use /Resources is if you have to go up a library first. like ../Resources
尝试删除您所在位置前面的正斜杠。您应该使用 /Resources 的唯一时间是您必须先上图书馆。像../资源
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/BiometricDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Hope this helps you.
希望这对你有帮助。