如何:如果映像来自嵌入式程序集,则在项目中使用它们

本文关键字:项目 映像 如果 自嵌 程序集 嵌入式 如何 | 更新日期: 2023-09-27 18:05:36

如何:在项目中使用图像,如果它们来自嵌入式程序集

我创建了一个程序集,它有一个使用images的公共方法。这些图像是嵌入的。现在,当我尝试在我的新项目中使用程序集时,除了图像之外,一切都很好。

我尝试在不使用程序集(只是一个普通的WPF应用程序)的情况下运行应用程序,它工作了。因此,程序集似乎没有找到嵌入的图像。

你能帮我吗?

编辑:我创建我的程序集中使用的图像的方式。

public void CreateBitmapImage(string uri)
    {
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.UriSource = new Uri(uri, UriKind.Absolute);
        bitmapImage.EndInit();
        image_msgIcon.Source = bitmapImage;
    }
    public void SetMsgImage(string msgImage)
    {
        if (msgImage == Error)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/cross-circle.png");
        }
        else if (msgImage == Exclamation)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/exclamation.png");
        }
        else if (msgImage == Information)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/information.png");
        }
        else if (msgImage == Question)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/question.png");
        }
        else
        {
        }
    }

现在我得到这个错误信息:

Die Ressource "images/custommessagebox/cross-circle.png" kann nicht gefunden werden.
The resource "images/custommessagebox/cross-circle.png" cannot be found.

我试过了:

CreateBitmapImage("/CL.New;component/Images/CustomMessageBox/cross-circle.png");

bitmapImage.UriSource = new Uri(uri, UriKind.Relative);

…这似乎可以工作,但是没有显示图像(没有错误消息)。

如何:如果映像来自嵌入式程序集,则在项目中使用它们

您应该阅读WPF文档中的包uri。

基本上,当你从不同的程序集引用资源时,你应该使用不同的Uri。如文档中所述,尝试使用如下内容:

pack://application:,,,/ReferencedAssembly;component/Image.png 

pack://application:,,,/ReferencedAssembly;component/Subfolder/Image.png