如何在VSPackage中设置值后刷新Visual Studio设置

dcs*_*raw 4 vspackage visual-studio-2012

在Visual Studio扩展中,我已经定义了一个VSPackage,其中包含许多命令.在其中一个命令的处理程序中,我使用以下代码设置用户设置:

SettingsManager settingsManager = new ShellSettingsManager(this);
WritableSettingsStore userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

userSettingsStore.SetBoolean("Text Editor", "Visible Whitespace", true);
Run Code Online (Sandbox Code Playgroud)

这成功地在注册表中设置的值(HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0Exp\Text Editor在隔离外壳的情况下),但编辑器不会自动获得通知的变化,即白色的空间仍然隐藏.编辑>高级>显示空白区域的菜单选项仍然会关闭.重新启动Visual Studio会获取更改.

如何告诉Visual Studio刷新其用户设置的状态,以便其他所有人都收到有关更改的通知?

cez*_*tko 6

当a ITextView打开时,我得到了正确的命令.这是重要的原因,如果ITextView它没有打开,在我看来命令失败了.更快的方法是创建一个Editor Margin扩展项目(必须安装VS SDK).在EditorMargin课堂上这样做:

    [Import]
    private SVsServiceProvider _ServiceProvider;

    private DTE2 _DTE2;

    public EditorMargin1(IWpfTextView textView)
    {
        // [...]

        _DTE2 = (DTE2)_ServiceProvider.GetService(typeof(DTE));

        textView.GotAggregateFocus += new EventHandler(textView_GotAggregateFocus);
    }

    void textView_GotAggregateFocus(object sender, EventArgs e)
    {
        _DTE2.Commands.Raise(VSConstants.CMDSETID.StandardCommandSet2K_string,
            (int)VSConstants.VSStd2KCmdID.TOGGLEVISSPACE, null, null);

        //  The following is probably the same
        // _DET2.ExecuteCommand("Edit.ViewWhiteSpace");
    }
Run Code Online (Sandbox Code Playgroud)

注意:IWpfTextViewCreationListener如果您不想创建保证金,那就足够了.了解MEF扩展以使用它.

现在,此设置可能在VS2010之前的工具 - >选项页面中进行了控制.可以使用DTE自动化控制该页面的其他选项:

_DTE2.Properties["TextEditor", "General"].Item("DetectUTF8WithoutSignature").Value = true;
_DTE2.Properties["Environment", "Documents"].Item("CheckLineEndingsOnLoad").Value = true;
Run Code Online (Sandbox Code Playgroud)

ShellSettingsManager只是写入注册表,没有设置刷新功能(如果它存在,无论如何它将无效,因为它将不得不重新加载整个设置集合).以前的那些是我正在寻找的.解决你的问题是一个奖金:)