Event when a Document Window is focus in Visual Studio

邮差的信 提交于 2019-12-19 07:45:10

问题


I have an extensibility project in Visual Studio and I need to use the event triggered when I change from one window to another in the visual studio editor, my problem:

I created a Tool Window that display some diagram, that diagram depend of an editable file, when I save the editable file my tool window updates the information, but when there is more than one editable file opened and I switch between them I want that the tool window updates the information as well. So:

I want to get the event triggered when I switch between windows, file or documents in Visual Studio so I can use it to execute the update code of my tool window. Is there something I can do about it?

I just read this question here but I didn't find a solution in there: Are there any document window focus events?


回答1:


You can subscribe to the EnvDTE.WindowEvents.WindowActivated event:

using EnvDTE;
using Microsoft.VisualStudio.Shell;

private class MyClass
{
    private DTE dte;

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

    private void OnWindowActivated(Window gotFocus, Window lostFocus)
    {
        throw new NotImplementedException();
    }
}

See for example the 1. Display document path of the active window in the status bar sample code.



来源:https://stackoverflow.com/questions/29001240/event-when-a-document-window-is-focus-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!