0

I'm trying to write an outliner for XML code in VS (an extension using their SDK from a VSIX template) and I would like to get an event-call whenever the user changes to a different codeview/document.

Then, I plan to check the document type, create and render an interactive outline if it is indeed an xml document.

How would I go about creating such a hooking and is it even necessary?

EDIT

I've attempted the following implementation, but I'm being told that object does not contain a definition for "GetGlobalService"

using System;
using System.Runtime.InteropServices;
using EnvDTE;
using Microsoft.VisualStudio.Shell;

[Guid("bc4c5e8f-a492-4a44-9e57-ec9ad945140e")]
public class OutlineWindow : ToolWindowPane
{

    private DTE dte;

    public OutlineWindow() : base(null)
    {
        this.Caption = "OutlineWindow";

        this.Content = new OutlineWindowControl();

        dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
        dte.Events.WindowEvents.WindowActivated += OnWindowActivated;
    }

    private void OnWindowActivated(Window gotFocus, Window lostFocus)
    {
        throw new NotImplementedException();
    }
}
TheLogan
  • 33
  • 1
  • 9
  • 1
    The VS internal structure changes a lot do this may not still be valid, but see [Event when a Document Window is focus in Visual Studio](https://stackoverflow.com/questions/29001240/event-when-a-document-window-is-focus-in-visual-studio) – stuartd Jan 22 '18 at 14:34
  • That looks like it could have worked, but I'm being told that 'object' does not contain a definition for 'Package.GetGlobalService' etc. Which is what I've run into with numerous of the ways I've attempted to do it earlier. – TheLogan Jan 22 '18 at 15:01
  • Maybe you're missing a cast. Can you edit your question with the relevant code? – stuartd Jan 22 '18 at 15:11
  • I just created a new empty class with the same code (I literally copy-pasted it from the class that was complaining), and it comes up with no errors, and from that I discovered that the problem was the inheritance from ToolWindowPane .. doh Thank you! :) – TheLogan Jan 22 '18 at 15:44

1 Answers1

1

Thanks to @stuartd I managed to get this working! My problem was actually that I was putting it in the wrong class, the inheritance was messing it up.

public class OutlineManager
{
    private DTE dte;

    public OutlineManager()
    {
        dte = Package.GetGlobalService(typeof(DTE)) as DTE;
        dte.Events.WindowEvents.WindowActivated += OnWindowActivated;
    }

    private void OnWindowActivated(Window gotFocus, Window lostFocus)
    {
        //This is run when a new "window"(panel) gains focus (not only the code window though)
    }
}
TheLogan
  • 33
  • 1
  • 9