Domanda

I have a Visual Studio extension which generates a script file and adds it to the current project.

I want to take advantage of the file preview feature in the Visual Studio 2012 Solution Explorer so that the file is previewed when it is added to the project (instead of actually opening the file).

This is the code I have so far:

UIHierarchy UIH = ((DTE2)dte).ToolWindows.SolutionExplorer;
UIHierarchyItem UIHItem = UIH.GetItem(@"MySolution\MyProject\MyClass.cs");
UIHItem.Select(vsUISelectionType.vsUISelectionTypeSelect);

This selects the item in the Solution Explorer however it does not cause the preview to be shown.

Is there any way to ask VS to show the preview?

È stato utile?

Soluzione

I've come up with a solution, but it's not exactly pretty.

Since there doesn't appear to be a specific command to show the file preview, an alternative is to toggle the "Preview Selected Items" button twice.

// Select the new item in the Solution Explorer
UIHierarchy UIH = ((DTE2)dte).ToolWindows.SolutionExplorer;
UIHierarchyItem UIHItem = UIH.GetItem(@"MySolution\MyProject\MyClass.cs");
UIHItem.Select(vsUISelectionType.vsUISelectionTypeSelect);

var guidStandardCommandSet11 = new Guid("{D63DB1F0-404E-4B21-9648-CA8D99245EC3}");
int cmdidToggleSingleClickPreview = 35;

// Activate the Solution Explorer to ensure the following commands are available
dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate(); 

// Toggle the Preview button in the Solution Explorer on/off or off/on
dte.Commands.Raise(guidStandardCommandSet11.ToString(), cmdidToggleSingleClickPreview, null, null);
dte.Commands.Raise(guidStandardCommandSet11.ToString(), cmdidToggleSingleClickPreview, null, null);

By toggling it twice, the preview is always displayed and the prior toggle status (set by the user) is always maintained.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top