2

我从 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 文件的命令?代码有问题吗?

4

1 回答 1

0

我看到了同样的问题,但重现略有不同。此代码适用于 .cpp 文件,但不适用于 .xml 文件:

EnvDTE.Solution soln = System.Activator.CreateInstance(
    Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;

soln.DTE.ItemOperations.OpenFile(file);
soln.DTE.ExecuteCommand("Edit.FormatDocument");

如果我用以下内容替换最后一行,则文件保持不变:

TextSelection selection = soln.DTE.ActiveDocument.Selection as TextSelection;
selection.SelectAll();
selection.SmartFormat();

我已经尝试了一段时间,但找不到解决方法。

于 2014-04-09T11:28:29.543 回答