2

ソリューション内のすべてのファイルをフォーマットする Visual Studio マクロから小さなマクロ スクリプトを派生させましたが、残念ながら xml、xaml、config などでは機能しませんProjectItem。プライマリ ビューで開く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 に答える