C# 使用 wpf 存储在外部 dll 中的参考图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2271863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:56:04  来源:igfitidea点击:

Reference images stored in external dll using wpf

c#wpfimagedllpng

提问by Matt B

I have a wpf application and a class library(dll) project. In my dll I have an images folder with some .png files set as Resource.

我有一个 wpf 应用程序和一个类库(dll)项目。在我的 dll 中,我有一个图像文件夹,其中一些 .png 文件设置为资源。

I want to reference and display the image using the wpf main application.

我想使用 wpf 主应用程序引用和显示图像。

Any ideas? I want to do this in the xaml not the code behind if at all possible.

有任何想法吗?如果可能的话,我想在 xaml 中执行此操作而不是后面的代码。

Ta, Matt.

塔,马特。

采纳答案by b-rad

Assuming you reference the class library from the WPF application you can reference and display the image in the WPF application with the following XAML:

假设您从 WPF 应用程序引用类库,您可以使用以下 XAML 在 WPF 应用程序中引用和显示图像:

<Image Source="/ClassLibraryName;Component/images/myimage.png"/>

The important thing here is "ClassLibraryName" which is the assembly name for your class library. "/images/myimage.png" is the path to your image.

这里重要的是“ClassLibraryName”,它是您的类库的程序集名称。“/images/myimage.png”是您的图像的路径。

You can find out more about WPF pack URIs here.

您可以在此处找到有关 WPF 包 URI 的更多信息。

回答by icernos

I was already using below but still didn't work.

我已经在下面使用了,但仍然没有用。

<Image Source="/ClassLibraryName;Component/images/myimage.png"/>

I changed the "Build Action" to "Resource" but still didn't work.

我将“构建操作”更改为“资源”,但仍然无效。

Finally, after cleaning the solution and rebuilding all, it worked!!!

最后,在清洁解决方案并重建所有后,它起作用了!!!

Setup: Microsoft Visual Studio Enterprise 2015, Windows 10 Pro, C#

安装程序:Microsoft Visual Studio Enterprise 2015、Windows 10 Pro、C#

回答by prw56

This did not work for me:

这对我不起作用:

<Image Source="/ClassLibraryName;Component/images/myimage.png"/>

But this did:

但这确实:

<Image Source="pack://application:,,/ClassLibraryName;Component/images/myimage.png"/>

I also cleaned and rebuilt prior to this, but only after adding this addition bit were the images retrieved.

在此之前我也清理和重建过,但只有在添加了这个附加位之后才检索到图像。

回答by khushal dawda

First In Class Library Project Set Images(actual Images) Build Type To Resources. Give this Class Library Reference to project whenever you want to use images. When u want this images code as Follow for wpf.

First In Class Library Project 将图像(实际图像)构建类型设置为资源。每当您想使用图像时,将此类库参考提供给项目。当你想要这个图像代码作为 wpf 的 Follow 时。

<Image Source="pack://application:,,,/YOUR_DLL_FILE_NAME;Component/Images/Splashscreen.png" Stretch="Fill" />

Images Is FOLDER NAME

图像是文件夹名称