2

我得出一个小的宏脚本从Visual Studio Macro to Format all Files in a Solution但遗憾的是它并没有使用XML,XAML,工作配置等所有ProjectItem是基于XML的通常抛出异常(命令不可用),当他们在他们的主要观点vsViewKindPrimary被打开:Visual Studio宏:如何格式化xml文件?

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文件?代码有问题吗?

+0

请添加任何德语信息的英文翻译,因为这主要是英语问题和答案。谢谢 – 2011-08-09 10:53:06

回答

0

我看到相同的问题,但有一个稍微不同的repro。此代码为.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(); 

我已经试过了而无法找到解决方法。