0

I have a Visual Studio extension that gets the value and then sets a value to the Text Editor -> General -> Track Changes setting in the Options dialog.

A code that was working fine with Visual Studio 2012-2017:

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

is throwing a COMException with the following message: "Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))" at EnvDTE._DTE.get_Properties(String Category, String Page) in Visual Studio 2019.

Apparently the setting is moved, so I, trying to get the new location, exported the settings to file in Visual Studio 2017 and 2019, and compared the results:

  • 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>
    

I am still not sure how to use the information, as the indexer of the DTE.Properties accepts two parameters: Category and Page. I already tried the following:

        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");

but without success.

4

1 回答 1

1

微软工作人员澄清说,可以使用以下任何一种方法:

  • 使用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);
    
  • 使用IEditorOptionsFactoryServiceMEF 服务。此 API 在 Visual Studio 2019 及更高版本中添加:

    <IEditorOptionsFactoryService>.GlobalOptions.GetOptionValue<bool>(DefaultTextViewHostOptions.ChangeTrackingId);
    <IEditorOptionsFactoryService>.GlobalOptions.SetOptionValue<bool>(DefaultTextViewHostOptions.ChangeTrackingId, <true/false>);
    
于 2019-05-03T18:41:15.077 回答