如何在 WPF 控件内的类库中使用 .resx 文件

本文关键字:resx 文件 类库 WPF 控件 | 更新日期: 2023-09-27 18:34:47

所以我有一个库来托管我的所有图像。这适用于我的 WinForms 控件;但是,我在 WPF 中使用同一类库的资源时遇到困难。

MyApp.Resources //Is my control library project.
Resources //Is the folder that contains all my images at the root of the project.

如何从类库中引用 WPF 控件内的图像?

我尝试过的:

<ImageBrush ImageSource="/MyApp.Resources;Resources/manage.png" />
-

-用户控件属性

Resources="pack://application:,,,/MyApp.Resources;Resources.resx">

如何在 WPF 控件内的类库中使用 .resx 文件

  1. 添加新的 RESX 文件。
  2. 在此文件中添加您的文件。
  3. 在 VS 的解决方案资源管理器中检查 RESX 文件,并将生成操作从"嵌入的资源"更改为"资源"。
  4. 打开 RESX 文件并将访问修饰符从"内部"更改为"公共"。
  5. 创建一个新的资源字典文件(例如。Files.xaml(:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
   <BitmapImage x:Key="someImage" 
                UriSource="/YourAppNamespace;component/Resources/image.png" />
</ResourceDictionary>
  1. 在窗口或应用资源中导入词典:
<ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Files.xaml" />
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
  1. 在 WPF 应用中使用图像:
<Image Source="{StaticResource someImage}" />