【问题标题】:How do I use a .resx file in a class library inside a WPF control?如何在 WPF 控件内的类库中使用 .resx 文件?
【发布时间】:2012-05-26 04:41:50
【问题描述】:

所以我有一个库来托管我的所有图像。这适用于我的 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" />

--UserControl 属性

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

【问题讨论】:

标签: c# wpf winforms class-library


【解决方案1】:
  1. 添加一个新的 RESX 文件。
  2. 在这个文件中添加你的文件。
  3. 在 VS 的解决方案资源管理器中检查您的 RESX 文件,并将构建操作从 Embedded Resource 更改为 Resource。
  4. 打开 RESX 文件并将访问修饰符从 Internal 更改为 Public。
  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}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多