How to update Visual Studio settings after setting a value in VSPackage

In the Visual Studio extension, I defined VSPackage with several commands in it. In the handler of one of the commands, I set a user parameter 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 (in HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0Exp\Text Editor in case of an isolated shell), but the editor is not automatically notified of the change, i.e. the space remains hidden. Also, the menu option "Edit"> "Advanced"> "Show White Space" is disabled. Restarting Visual Studio picks up the changes.

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

+4
source share
1 answer

I got the correct command when opening ITextView . This is an important reason, if ITextView does not open, it seems to me that the command simply fails. A faster way is to create a Margin Editor extension project (the VS SDK must be installed). In the EditorMargin class EditorMargin do the following:

  [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 do not want to create Margin. Learn about the MEF extensions to use it.

Now this parameter was probably controlled on the Tools → Options page until VS2010. Other parameters of this page can be controlled using DTE automation:

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

ShellSettingsManager only refers to writing to the registry, there is no function for updating settings (if it exists, it would be inefficient, anyway, it would lead to a reboot of the entire collection of settings). The previous ones were what I was looking for. Solving your problem was a bonus :)

+6
source

All Articles