我想出了一个解决方案,但它并不完全漂亮。
由于似乎没有显示文件预览的特定命令,因此另一种方法是切换“预览选定项目”按钮两次。
// 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);
通过切换两次,始终显示预览并始终保持先前的切换状态(由用户设置)。