问题
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);
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.
How can I tell Visual Studio to refresh the state of its user settings so that everything else gets notified of the change?
回答1:
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");
}
Note: IWpfTextViewCreationListener
should be enough if you don't want to create a Margin. Learn about MEF extensions to use it.
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
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 :)
来源:https://stackoverflow.com/questions/14615524/how-to-refresh-visual-studio-settings-after-setting-a-value-in-a-vspackage