【问题标题】:Getting/setting Track Changes setting property value trough DTE in Visual Studio 2019在 Visual Studio 2019 中通过 DTE 获取/设置 Track Changes 设置属性值
【发布时间】:2019-05-09 17:14:30
【问题描述】:

我有一个 Visual Studio 扩展,它获取值,然后将值设置为 文本编辑器 -> 常规 -> 选项对话框中的跟踪更改设置。

在 Visual Studio 2012-2017 中运行良好的代码:

DTE vsEnvironment = (DTE)GetService(typeof(DTE));
Property trackChangesProperty = vsEnvironment.Properties["TextEditor", "General"].Item("TrackChanges");

在 Visual Studio 2019 的 EnvDTE._DTE.get_Properties(String Category, String Page) 处抛出带有以下消息的 COMException:“无效索引。(来自 HRESULT 的异常:0x8002000B (DISP_E_BADINDEX))”。

显然设置已移动,所以我尝试获取新位置,将设置导出到 Visual Studio 2017 和 2019 中的文件,并比较结果:

  • Visual Studio 2017:

    <ToolsOptionsCategory name="TextEditor" RegisteredName="TextEditor">
    <ToolsOptionsSubCategory name="General" RegisteredName="General" PackageName="Text Management Package">
        <PropertyValue name="TrackChanges">true</PropertyValue>
    </ToolsOptionsSubCategory>
    

  • Visual Studio 2019:

    <Category name="Text Editor_General" Category="{c178af61-531a-46f0-bd57-102d9e42c711}" Package="{e269b994-ef71-4ce0-8bcd-581c217372e8}" RegisteredName="Text Editor_General" PackageName="Microsoft.VisualStudio.Editor.Implementation.EditorPackage">
    <PropertyValue name="TrackChanges">true</PropertyValue>
    

我仍然不确定如何使用这些信息,因为DTE.Properties 的索引器接受两个参数:Category 和Page。我已经尝试了以下方法:

        vsEnvironment.Properties["TextEditor", null].Item("TrackChanges");
        vsEnvironment.Properties["TextEditor", string.Empty].Item("TrackChanges");
        vsEnvironment.Properties["Text Editor_General", null].Item("TrackChanges");
        vsEnvironment.Properties["Text Editor_General", string.Empty].Item("TrackChanges");

但没有成功。

【问题讨论】:

    标签: visual-studio envdte visual-studio-2019


    【解决方案1】:

    Microsoft 员工clarified 表示可以使用以下任何一种方法:

    • 使用IVsTextManager3.SetUserPreferences3()。旧版本的 Visual Studio 也可用(我使用 Visual Studio 2012 - 2019 进行了测试),但 API 非常难看:

      IVsTextManager3 textManager = this.GetService(typeof(VsTextManagerClass)) as IVsTextManager3;
      
      VIEWPREFERENCES3[] viewPreferences3Array = new VIEWPREFERENCES3[1];
      FONTCOLORPREFERENCES2[] fontColorPreferences2Array = new FONTCOLORPREFERENCES2[1];
      FRAMEPREFERENCES2[] framePreferences2Array = new FRAMEPREFERENCES2[1];
      LANGPREFERENCES2[] langPreferences2Array = new LANGPREFERENCES2[1];
      
      textManager.GetUserPreferences3(viewPreferences3Array, framePreferences2Array, langPreferences2Array, fontColorPreferences2Array);
      
      VIEWPREFERENCES3 viewPreferences3 = viewPreferences3Array[0];
      viewPreferences3.fTrackChanges = 0;
      textManager.SetUserPreferences3(new VIEWPREFERENCES3[] { viewPreferences3 }, framePreferences2Array, langPreferences2Array, fontColorPreferences2Array);
      
    • 使用IEditorOptionsFactoryService MEF 服务。此 API 在 Visual Studio 2019 及更高版本中添加:

      <IEditorOptionsFactoryService>.GlobalOptions.GetOptionValue<bool>(DefaultTextViewHostOptions.ChangeTrackingId);
      <IEditorOptionsFactoryService>.GlobalOptions.SetOptionValue<bool>(DefaultTextViewHostOptions.ChangeTrackingId, <true/false>);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      相关资源
      最近更新 更多