微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 VS2022 中将 ImageMoniker 转换为 WPF BitmapSource

如何解决在 VS2022 中将 ImageMoniker 转换为 WPF BitmapSource

我正在为 Visual Studio 开发一个扩展,它在 VS 窗口中包含一个 XAML 视图。我希望扩展看起来像原生 UI。该扩展目前在 VS2017 和 VS2019 中运行良好,使用以下代码将名字对象转换为可直接从 XAML 使用的 WPF BitmapSource:

public static BitmapSource GetIconForImageMoniker(ImageMoniker? imageMoniker,int sizeX,int sizeY)
{
    if (imageMoniker == null)
    {
        return null;
    }

    IVsImageService2 vsIconService = ServiceProvider.GlobalProvider.GetService(typeof(SVsImageService)) as IVsImageService2;

    if (vsIconService == null)
    {
        return null;
    }

    ImageAttributes imageAttributes = new ImageAttributes
    {
        Flags = (uint)_ImageAttributesFlags.IAF_requiredFlags,ImageType = (uint)_UIImageType.IT_Bitmap,Format = (uint)_UIDataFormat.DF_WPF,LogicalHeight = sizeY,LogicalWidth = sizeX,StructSize = Marshal.SizeOf(typeof(ImageAttributes))
    };

    IVsUIObject result = vsIconService.Getimage(imageMoniker.Value,imageAttributes);

    object data;
    result.get_Data(out data);
    BitmapSource glyph = data as BitmapSource;

    if (glyph != null)
    {
        glyph.Freeze();
    }

    return glyph;
}

方法是从 WpfUtil 类中直接复制粘贴的,可在 Mads Kristensen 的多个扩展中使用。

如前所述,这在 VS2017 和 VS2019 中运行良好。现在我也希望它在 VS2022 中运行。该扩展程序显示在 VS2022 中,但不再显示图标。问题是这在 VS2022 中返回 null,但在以前的版本中不返回:

ServiceProvider.GlobalProvider.GetService(typeof(SVsImageService)) as IVsImageService2;

有谁知道如何在 VS2022 中实现这个功能

解决方法

这是由 VS2022 中互操作库的更改引起的。也就是说,它们都已合并到一个库中(您可以查看详细信息 here)。

这确实破坏了与以前版本的 Visual Studio 的兼容性。有一个 guide for migrating extensions to VS2022,但总而言之,指导是:

  1. 将源代码重构为共享项目。
  2. 创建一个针对 VS2022 的新 VSIX 项目,您现在拥有的 VSIX 项目将保留用于针对以前的版本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。