访问 VS 扩展中的 XAML 内容

Accessing XAML content in VS extension

我目前正在开发一个扩展程序来帮助我扫描我的代码,尤其是 XAML 文件。关于我的问题的代码如下所示:

  For Each file As ProjectItem In SolutionFiles()
     If file.Name.EndsWith(".xaml") Then
        Dim win As Window = file.Open(EnvDTE.Constants.vsViewKindCode)

        For Each elem As CodeElement In win.ProjectItem.FileCodeModel.CodeElements
           Dim strLine() As String = elem.StartPoint.CreateEditPoint().GetText(elem.EndPoint).Split("vbcrlf")
           Dim Linecount As Integer = 0
           For Each line As String In strLine
           ...
           Next
        Next
     End If
  Next

我开始意识到 file.Open(EnvDTE.Constants.vsViewKindCode) 给我关联的 xaml.vb 代码,而不是 xaml 代码本身。但是当我尝试使用 file.Open(EnvDTE.Constants.vsViewKindDesigner) 时,win.ProjectItem.FileCodeModel 什么都没有。

感谢任何帮助。谢谢。 :)

得到答案:

 Dim codeWin As Window = file.Open(EnvDTE.Constants.vsViewKindPrimary) 
 Dim fileName As String = If(codeWin IsNot Nothing, codeWin .Document.Path & file.Name, Nothing)
 Dim content As String = If(Not String.IsNullOrEmpty(fileName), System.IO.File.ReadAllText(fileName), Nothing)