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

本文关键字:资源 字典 元素 代码 访问 | 更新日期: 2023-09-27 17:58:56

如何在c#中进行编码以访问资源中的Imagemy_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>

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

假设您的ResourceDictionary已加载,您可以执行以下操作:

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