按名称访问c#代码的资源字典的元素

时间:2013-02-12 23:03:15

标签: c# wpf dictionary resourcedictionary

如何在c#中编码以访问资源中的Image my_image_name ? 因为我给了资源字典名称 mydic_name 。我可以按名称参考字典吗?

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="mydic_name">
                    <Image x:Name="my_image_name" x:Key="my_image_key" Source="Properties/images/device1.png"/>
</ResourceDictionary>

1 个答案:

答案 0 :(得分:2)

假设你的ResourceDictionary已加载,你可以这样做:

 var image = (Image)Application.Current.Resources.FindName("my_image_name");