包含 Prism 模块的图标

发布于 2024-10-10 17:58:43 字数 528 浏览 4 评论 0原文

我希望我的 Prism shell 显示它发现的每个模块的图像和标签。我该如何包含模块组件中的图像并在 shell 中访问它?

我已经尝试创建一个接口来提供图标(作为 ImageSource)和字符串标签,但我在从模块的图像创建 ImageSource 时遇到了问题集会。 (BitmapImage 构造函数中使用的 URI 始终希望在 shell 程序集中查找内容,而不是在模块程序集中查找内容。)

我想我可以添加图像作为资源并使用它,但它在代码中表示为 System.Drawing.Bitmap,并且我找不到明显的方法将其转换为 WPF shell 中可用的类型。 (我确实看到一些代码用于从Bitmap,但这感觉可能是错误的方法。)[编辑:这种方法确实有效,但我仍然认为它有味道。]

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?

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.)

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.) [Edit: That approach does actually work, but I still think it smells.]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水微香 2024-10-17 17:58:43

帕特,你的问题很可能是因为你没有使用正确的图像路径。您试图在视图构造函数或其他内容中加载该图像,但它没有发现导入失败。
您想仔细检查您的图像是否被编译为资源。
使用它(假设您的图像名为 Icon.png 并且位于模块项目的根目录中)

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;
    }
}

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;
    }
}
夜唯美灬不弃 2024-10-17 17:58:43

当您从模块返回 BitmapSource 时,请确保使用 打包 Uri。如果您尝试从程序集 ModuleFoo.dll 加载图像,您将执行如下操作:

public ImageSource ModuleImage
{
    get 
    {
        return BitmapFrame.Create(new Uri("pack://application:,,,/ModuleFoo;component/ModuleIcon.png", UriKind.Absolute); 
    }
}

请记住,对于 WPF 资源,图像必须编译为“资源”,而不是 WinForms 所需的“嵌入式资源”。

When you return the BitmapSource from the module, make sure to use a Pack Uri. If you are trying to load an image from an assembly ModuleFoo.dll, you would do something like so:

public ImageSource ModuleImage
{
    get 
    {
        return BitmapFrame.Create(new Uri("pack://application:,,,/ModuleFoo;component/ModuleIcon.png", UriKind.Absolute); 
    }
}

Remember that for WPF resources, the images must be compiled as a "Resource", and not "Embedded Resource" that WinForms requires.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文