Search code examples
vb.netxamlvisual-studio-extensions

Accessing XAML content in VS extension


I am currently developping an extension that helps me scan my code, especially XAML files. The code concerning my problem looks like that:

  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

I came to realise that file.Open(EnvDTE.Constants.vsViewKindCode) gives me the associated xaml.vb code and not the xaml code itself. But when I try using file.Open(EnvDTE.Constants.vsViewKindDesigner), the win.ProjectItem.FileCodeModel is Nothing.

Any help appreciated. Thanks. :)


Solution

  • Got the answer for that:

     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)