【问题标题】:Visual Studio 2017: Event-call when user changes tabVisual Studio 2017:用户更改选项卡时的事件调用
【发布时间】:2018-07-01 04:57:39
【问题描述】:

我正在尝试为 VS 中的 XML 代码编写大纲(使用 VSIX 模板中的 SDK 的扩展),并且我希望在用户更改为不同的代码视图/文档时获得事件调用。

然后,我计划检查文档类型,如果确实是 xml 文档,则创建并呈现交互式大纲。

我将如何创建这样的挂钩,甚至有必要吗?

编辑

我尝试了以下实现,但我被告知对象不包含“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();
    }
}

【问题讨论】:

  • VS 内部结构发生了很大变化,这样做可能仍然无效,但请参阅Event when a Document Window is focus in Visual Studio
  • 看起来它可以工作,但我被告知'object'不包含'Package.GetGlobalService'等的定义。这是我遇到的许多我之前尝试过的方法。
  • 也许你错过了一个演员表。您可以使用相关代码编辑您的问题吗?
  • 我刚刚使用相同的代码创建了一个新的空类(我实际上是从抱怨的类中复制粘贴它),它没有出现任何错误,从那我发现了问题是 ToolWindowPane 的继承 .. doh 谢谢! :)

标签: c# visual-studio-2017 rendering extending outliner


【解决方案1】:

感谢@stuartd,我成功地完成了这项工作! 我的问题实际上是我把它放在了错误的类中,继承把它搞砸了。

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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    相关资源
    最近更新 更多