我从 Visual Studio Macro 派生了一个小宏脚本来格式化解决方案中的所有文件,但不幸的是它不适用于 xml、xaml、config 等。所有ProjectItem
基于 xml 的通常都会抛出异常(命令不可用)。在他们的主要视图中打开vsViewKindPrimary
:
Dim projectItem As ProjectItem ' actually this is a parameter of a sub'
Dim window As Window = projectItem.Open(Constants.vsViewKindPrimary)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
window.Close(vsSaveChanges.vsSaveChangesYes) ' actually this is part of a finally block'
结果是:
System.Runtime.InteropServices.COMException (0x80004005): Command "Edit.FormatDocument" is not available.
at EnvDTE.DTEClass.ExecuteCommand(String CommandName, String CommandArgs)
当它们作为文本打开时,vsViewKindTextView
它们保持原样,尽管Edit.FormatDocument
可以执行。
是否有另一个必须用于 xml 文件的命令?代码有问题吗?