在 VSPackage 中设置值后如何刷新 Visual Studio 设置 [英] How to refresh Visual Studio settings after setting a value in a VSPackage

查看:25
本文介绍了在 VSPackage 中设置值后如何刷新 Visual Studio 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a Visual Studio extension I have defined a VSPackage with a number of commands in it. In the handler for one of the commands, I set a user setting using the following code:

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

userSettingsStore.SetBoolean("Text Editor", "Visible Whitespace", true);

这成功设置了注册表中的值(在隔离外壳的情况下位于 HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0Exp\Text Editor),但编辑器不会自动收到通知的变化,即空白保持隐藏.此外,编辑 > 高级 > 显示空白处的菜单选项保持关闭状态.重新启动 Visual Studio 即可完成更改.

This successfully sets the value in the registry (at HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0Exp\Text Editor in the case of the isolated shell), but the editor does not automatically get notified of the change, i.e. the white space remains hidden. Also the menu option at Edit > Advanced > Show White Space remains toggled off. Restarting Visual Studio picks up the change.

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

How can I tell Visual Studio to refresh the state of its user settings so that everything else gets notified of the change?

推荐答案

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

I got it raising the right command when a ITextView is opened. This is important cause if a ITextView it's not opened it seems to me the command just fails. The quicker way is to create an Editor Margin extension project (VS SDK must be installed). On the EditorMargin class do this:

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

注意:如果您不想创建 Margin,IWpfTextViewCreationListener 应该足够了.了解 MEF 扩展以使用它.

Note: IWpfTextViewCreationListener should be enough if you don't want to create a Margin. Learn about MEF extensions to use it.

现在,这个设置可能是在 VS2010 之前的 Tools -> Options 页面中控制的.该页面的其他选项可以通过 DTE 自动化进行控制:

Now, this setting was probably controlled in Tools -> Options page prior to VS2010 . Other options of that page can be controlled with DTE automation:

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

ShellSettingsManager 仅用于写入注册表,没有设置刷新功能(如果存在,则效率不高,因为它必须重新加载整个设置集合).以前的那些是我正在寻找的.解决您的问题是一个奖励:)

ShellSettingsManager is only about writing to the registry, there's no settings refresh function (if it exists, it would be not efficient anyway cause it would have to reload the entire collection of settings). The previous ones were what I was looking for. Solving your problem was a bonus :)

这篇关于在 VSPackage 中设置值后如何刷新 Visual Studio 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆