简体   繁体   English

在扩展中编写Visual Studio设置不会保留

[英]Writing Visual Studio settings in an extension do not stay

In my extension that I am writing for Visual Studio 2015 I want to change the tab size and indent size as at work we have a different setting as when I am developing for opensource project (company history dating our C period). 在我为Visual Studio 2015编写的扩展中,我希望更改选项卡大小和缩进大小,因为在工作中我们有不同的设置,就像我正在开发开源项目(公司历史记录在我们的C期间)。 I have written the following code in my command class: 我在命令类中编写了以下代码:

private const string CollectionPath = @"Text Editor\CSharp";
private void MenuItemCallback(object sender, EventArgs e)
{
  var settingsManager = new ShellSettingsManager(ServiceProvider);
  var settingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
  var tabSize = settingsStore.GetInt32(CollectionPath, "Tab Size", -1);
  var indentSize = settingsStore.GetInt32(CollectionPath, "Indent Size", -1);
  if (tabSize != -1 && indentSize != -1)
  {
    settingsStore.SetInt32(CollectionPath, "Tab Size", 2);
    settingsStore.SetInt32(CollectionPath, "Indent Size", 2);
  }
}

When testing in an experimental hive it changes it when you step through the method but when you open the Options dialog it stays the original values. 在实验配置单元中进行测试时,当您单步执行该方法时,它会更改它,但是当您打开“选项”对话框时,它将保持原始值。 When you debug again the values stay the original. 再次调试时,值保持原始值。

What did I forget or did wrong? 我忘记了什么或做错了什么?

Directly access the Visual Studio options via the Properties functionality in the EnvDTE assembly. 通过EnvDTE程序集中的“ Properties功能直接访问Visual Studio选项。

private void ChangeTabs(DTE vs, int newTabSize, int newIndentSize)
{
    var cSharp = vs.Properties["TextEditor", "CSharp"];

    EnvDTE.Property lTabSize = cSharp.Item("TabSize");
    EnvDTE.Property lIndentSize = cSharp.Item("IndentSize");

    lTabSize.Value = newTabSize;
    lIndentSize.Value = newIndentSize;
}

private void ChangeSettings()
{
   DTE vs = (DTE)GetService(typeof(DTE)); 
   ChangeTabs(vs, 3, 3);
}

For reference: Controlling Options Settings 供参考: 控制选项设置

To be complete. 要完整。 This is the correct answer: 这是正确的答案:

In the constructor you need to add 在构造函数中,您需要添加

_dte2 = (DTE2) ServiceProvider.GetService(typeof (DTE));

And with the command it is like this 并且使用命令就像这样

    _dte2.Properties["TextEditor", "CSharp"].Item("TabSize").Value = 2;
    _dte2.Properties["TextEditor", "CSharp"].Item("IndentSize").Value = 2;
    _dte2.Commands.Raise(VSConstants.CMDSETID.StandardCommandSet2K_string, (int)VSConstants.VSStd2KCmdID.FORMATDOCUMENT, null, null);

There is an issue that the changed options are overridden with the defaults when you restart Visual Studio. 重新启动Visual Studio时,存在一个问题,即使用默认值覆盖已更改的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM