我正在为Visual开发一个扩展,它在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的多个扩展中使用。
如前所述,这在VS2017和VS2019中工作得很好。现在,我也希望这在VS2022中运行。扩展显示在VS2022中,但图标不再显示。问题是,这在VS2022中返回VS2022,但在以前的版本中不返回:
ServiceProvider.GlobalProvider.GetService(typeof(SVsImageService)) as IVsImageService2;有人知道如何在VS2022中实现这一点吗?
发布于 2021-07-08 16:36:06
这是由于对VS2022中的互操作库的更改造成的。也就是说,它们都被合并到一个库中(您可以看到细节这里)。
这确实破坏了与Visual以前版本的目标兼容。有一个迁移到VS2022的扩展指南,但概括地说,指南是:
https://stackoverflow.com/questions/68065717
复制相似问题