Class 库是用于将图像存储为 WPF 项目要使用的资源的正确项目吗
Is A Class Library The Correct Project To Use To Store Images As Resources To Be Used By A WPF Project
我已将一些 .jpg 文件加载到 Visual Studio 2015 下的 class 库项目中,并将它们设置为构建为资源并在新建时复制。当我将它用于我的 WPF 项目时,它似乎工作正常。有没有另一种方法可以将图像存储到 DLL 中,或者这种方法是否正确?
======================================
2016 年 3 月 22 日更新 5:02 下午
为了清楚起见,我将 ImageSource 语句添加到我的 App.xaml 文件中作为 Application.Resources 部分,如下所示:
<ImageSource x:Key="OkImage">pack://application:,,,/MyStyles;component/ok.png</ImageSource>
然后我在我的 WPF window xaml 中引用 OkImage 关键字,如下所示用于图像控件:
<Image x:Name="image" Source="{DynamicResource OkImage}"
Grid.Column="1" HorizontalAlignment="Left" Height="100" Margin="66,25,0,0" Grid.Row="2" VerticalAlignment="Top" Width="100"/>
这就是我将图像从 .DLL 中提取到 WPF 文件中的方法。带有图像的 .DLL 的目的是与其他人共享,以便在当前和未来的项目的控件上使用相同的图像。
我会使用一个资源文件(或多个资源文件)来存储这些数据。在项目中创建一个新文件夹并将其命名为 Resources(名称并不重要)。然后右键单击新文件夹并单击添加 -> 新项目 -> 和 select "Resource file"(见图 #1)。给它起个名字,然后你的新文件夹中应该有一个 something.resx 文件(见图 #2)。然后打开新创建的资源文件,您可以看到在编辑器的左上角有一个选项可以将现有文件(浏览文件系统中的文件)添加到 resx 文件中。添加文件后,您可以使用命名空间和为它们自动创建的 class 在源代码中将它们作为 image/string/audio/etc 文件访问。我在下面包含了一个代码示例。
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Bitmap myUntitledImage = WpfApplication1.Resources.Resource1.Untitled; //Untitled is the name of my image
}
}
}
方法没有错,但我认为你需要考虑一种情况,你可能想要升级这个DLL(一张或多张图片)。如果您在其中存储许多图片作为资源,那么 DLL 的大小将会增加,因此,如果您更改 DLL,则需要重新分发一个新的 DLL 给您的所有客户端。如果不是这样,您可以将图片存储在外部 DLL 中。
关于性能的考虑,资源缓存在内存中。资源使用 System.Resources.ResourceManager,这会缓存图像和字符串。因此,您存储在资源程序集中的所有图像都将存储在内存中。这是您需要考虑的另一点。
Is A Class Library The Correct Project To Use To Store Images As Resources To Be Used By A WPF Project
当然! WPF 将其称为 资源文件 - 标记为 Resource 的 Build Action 的文件。这些资源然后被编译到程序集中,可以从 c# 代码或通过 XAML 通过 Pack URIs 加载它们。这就是 WPF 应用程序不应使用 WinForms 样式 .resx 文件的原因,因为 XAML 通常无法使用它们。
- 当资源在编译时已知或在构建或部署后不需要更新它们时,您可以使用资源文件。
- 资源文件减少了要分发的文件数量
MSDN:
If an application data file must always be available to an application, the only way to guarantee availability is to compile it into an application's main executable assembly or one of its referenced assemblies. This type of application data file is known as a resource file. - Tell me more
原帖:
I have loaded a few .jpg files into a class library project under Visual Studio 2015 and set them to build as resource and copy when new
在将文件标记为 Resource 时,不要将它们标记为 “新建时复制”。这违背了资源文件的目的。您只是在复制一个很可能未被使用的附加图像文件。程序集中应嵌入资源文件。
原帖:
Is there another way to store images into a DLL or is this method correct?
您还可以使用内容文件。当文件可能在事后更改但您不想重新编译您的应用程序时,此技术很有用。您的应用程序对它在运行时评估的依赖资源有一个 link。 Tell me more about content files
原始文件站点
我必须承认我刚刚读到它时对此知之甚少。当构建时资源未知时,它似乎很有用;对于访问受限的部分信任应用程序很有用。完全信任的应用程序可能会发现 SoOF 语法比通常需要完整路径的 file://
更有用。
MSDN 是这样描述 SoOF 的:
...there are times when you may want to establish either an implicit or non-existent relationship between an assembly and an application data file, including when:
- A file doesn't exist when at compile time.
- You don't know what files your assembly will require until run time.
- You want to be able to update files without recompiling the assembly that they are associated with.
- Your application uses large data files, such as audio and video, and you only want users to download them if they choose to.
我已将一些 .jpg 文件加载到 Visual Studio 2015 下的 class 库项目中,并将它们设置为构建为资源并在新建时复制。当我将它用于我的 WPF 项目时,它似乎工作正常。有没有另一种方法可以将图像存储到 DLL 中,或者这种方法是否正确?
======================================
2016 年 3 月 22 日更新 5:02 下午
为了清楚起见,我将 ImageSource 语句添加到我的 App.xaml 文件中作为 Application.Resources 部分,如下所示:
<ImageSource x:Key="OkImage">pack://application:,,,/MyStyles;component/ok.png</ImageSource>
然后我在我的 WPF window xaml 中引用 OkImage 关键字,如下所示用于图像控件:
<Image x:Name="image" Source="{DynamicResource OkImage}"
Grid.Column="1" HorizontalAlignment="Left" Height="100" Margin="66,25,0,0" Grid.Row="2" VerticalAlignment="Top" Width="100"/>
这就是我将图像从 .DLL 中提取到 WPF 文件中的方法。带有图像的 .DLL 的目的是与其他人共享,以便在当前和未来的项目的控件上使用相同的图像。
我会使用一个资源文件(或多个资源文件)来存储这些数据。在项目中创建一个新文件夹并将其命名为 Resources(名称并不重要)。然后右键单击新文件夹并单击添加 -> 新项目 -> 和 select "Resource file"(见图 #1)。给它起个名字,然后你的新文件夹中应该有一个 something.resx 文件(见图 #2)。然后打开新创建的资源文件,您可以看到在编辑器的左上角有一个选项可以将现有文件(浏览文件系统中的文件)添加到 resx 文件中。添加文件后,您可以使用命名空间和为它们自动创建的 class 在源代码中将它们作为 image/string/audio/etc 文件访问。我在下面包含了一个代码示例。
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Bitmap myUntitledImage = WpfApplication1.Resources.Resource1.Untitled; //Untitled is the name of my image
}
}
}
方法没有错,但我认为你需要考虑一种情况,你可能想要升级这个DLL(一张或多张图片)。如果您在其中存储许多图片作为资源,那么 DLL 的大小将会增加,因此,如果您更改 DLL,则需要重新分发一个新的 DLL 给您的所有客户端。如果不是这样,您可以将图片存储在外部 DLL 中。
关于性能的考虑,资源缓存在内存中。资源使用 System.Resources.ResourceManager,这会缓存图像和字符串。因此,您存储在资源程序集中的所有图像都将存储在内存中。这是您需要考虑的另一点。
Is A Class Library The Correct Project To Use To Store Images As Resources To Be Used By A WPF Project
当然! WPF 将其称为 资源文件 - 标记为 Resource 的 Build Action 的文件。这些资源然后被编译到程序集中,可以从 c# 代码或通过 XAML 通过 Pack URIs 加载它们。这就是 WPF 应用程序不应使用 WinForms 样式 .resx 文件的原因,因为 XAML 通常无法使用它们。
- 当资源在编译时已知或在构建或部署后不需要更新它们时,您可以使用资源文件。
- 资源文件减少了要分发的文件数量
MSDN:
If an application data file must always be available to an application, the only way to guarantee availability is to compile it into an application's main executable assembly or one of its referenced assemblies. This type of application data file is known as a resource file. - Tell me more
原帖:
I have loaded a few .jpg files into a class library project under Visual Studio 2015 and set them to build as resource and copy when new
在将文件标记为 Resource 时,不要将它们标记为 “新建时复制”。这违背了资源文件的目的。您只是在复制一个很可能未被使用的附加图像文件。程序集中应嵌入资源文件。
原帖:
Is there another way to store images into a DLL or is this method correct?
您还可以使用内容文件。当文件可能在事后更改但您不想重新编译您的应用程序时,此技术很有用。您的应用程序对它在运行时评估的依赖资源有一个 link。 Tell me more about content files
原始文件站点
我必须承认我刚刚读到它时对此知之甚少。当构建时资源未知时,它似乎很有用;对于访问受限的部分信任应用程序很有用。完全信任的应用程序可能会发现 SoOF 语法比通常需要完整路径的 file://
更有用。
MSDN 是这样描述 SoOF 的:
...there are times when you may want to establish either an implicit or non-existent relationship between an assembly and an application data file, including when:
- A file doesn't exist when at compile time.
- You don't know what files your assembly will require until run time.
- You want to be able to update files without recompiling the assembly that they are associated with.
- Your application uses large data files, such as audio and video, and you only want users to download them if they choose to.