在Visual Studio扩展中,我已经定义了一个VSPackage,其中包含许多命令。在其中一个命令的处理程序中,我使用以下代码设置用户设置:
SettingsManager settingsManager = new ShellSettingsManager(this);
WritableSettingsStore userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
userSettingsStore.SetBoolean("Text Editor", "Visible Whitespace", true);
这成功地设置了注册表中的值(在隔离shell的情况下为HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0Exp\Text Editor
),但编辑器不会自动获得更改通知,即空白区域仍然隐藏。此外,编辑>中的菜单选项高级>显示白色空间仍然切换。重新启动Visual Studio会获取更改。
如何告诉Visual Studio刷新其用户设置的状态,以便其他所有人都收到有关更改的通知?
答案 0 :(得分:6)
当打开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");
}
注意:如果您不想创建保证金,IWpfTextViewCreationListener
就足够了。了解MEF扩展以使用它。
现在,此设置可能在工具中控制 - > VS2010之前的选项页面。可以使用DTE自动化控制该页面的其他选项:
_DTE2.Properties["TextEditor", "General"].Item("DetectUTF8WithoutSignature").Value = true;
_DTE2.Properties["Environment", "Documents"].Item("CheckLineEndingsOnLoad").Value = true;
ShellSettingsManager
只是写入注册表,没有设置刷新功能(如果它存在,它无论如何都不会有效,因为它必须重新加载整个设置集合)。以前的那些是我正在寻找的。解决你的问题是一个奖励:)