包括用于棱镜模块的图标棱镜、图标、模块

2023-09-07 09:32:22 作者:熊大对熊二说熊要有個熊样

我希望我的棱镜的外壳可以显示每个它发现模块中的图像和标签。我怎么可能会去,包括图像从一个模块组装和访问它的壳呢?

I want my Prism shell to display an image and label for each module that it discovers. How might I go about including an image from a module assembly and accessing it in the shell?

我已经尝试过提供一个图标创建一个接口(作为的ImageSource )和一个字符串的标签,但我有创造的的ImageSource 从模块的组装的形象。 (该URI用于构造一个的BitmapImage 总想找到shell的组件,而不是模块的组件内容。)

I've already tried creating an interface for providing an icon (as an ImageSource) and a string label, but I am having trouble creating the ImageSource from the image in the module's assembly. (The URI used in the constructor for a BitmapImage always wants to find content in the shell's assembly rather than the module's assembly.)

我在想,我可以添加图像作为一种资源,只使用,但它重新psented在code为$ P $ System.Drawing.Bitmap ,我无法找到一个明显的方法将其转换成一个类型在WPF的外壳使用。 (我没有看到some code 从一个位图,但感觉就像是错误的做法可能不得不接受这一说法,虽然)

I was thinking that I could add an image as a resource and just use that, but it is represented in code as a System.Drawing.Bitmap and I couldn't find an obvious way to convert that to a type usable in the WPF shell. (I did see some code for converting from a Bitmap, but that feels like the wrong approach. Might have to settle with that, though.)

推荐答案

帕特,你的问题很可能是因为你没有使用正确的路径图像。你要加载的视图中的构造函数或东西,图像,然后它没有找到导入失败。 你想仔细检查你的形象是否被编译为资源。 使用这个(假设你的图像被称为的icon.png并locatd在模块项目的根目录)

Pat, your problem is most likely because you do not use the correct path to the image. You are trying to load that image in the view constructor or something then it does not find the import fails. You wanna double check whether your image is compiled as Resource. Use this (assuming your image is called Icon.png and is locatd in the root of your module project)

public BitmapImage Icon
{
    get
    {
        var assembly = Assembly.GetCallingAssembly();

        Uri uri = new Uri("/" + GetType().Assembly.ToString().Split(',')[0] + ";component/Icon.png", UriKind.Relative);

        if (_icon == null)
            _icon = new BitmapImage(uri);

        return _icon;
    }
}