【问题标题】:Can't get access to the Events in Visual Studio Community Toolkit无法访问 Visual Studio Community Toolkit 中的事件
【发布时间】:2022-01-18 21:47:42
【问题描述】:

我正在尝试将旧的 Visual Studio 扩展迁移到新的 2022 Studio。找到了一些名为“Community Visual Studio Toolkit”的精美解决方案,但遇到了一些问题。当我在用户打开某些解决方案时使用ProvideAutoLoad 属性加载我的扩展时,我无法访问我需要签署我的事件处理程序的WindowEvents。这是调试时的错误:https://snipboard.io/yUXIed.jpg

所以这是我使用的代码,这里我有错误:

[ProvideAutoLoad(UIContextGuids80.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
public sealed class MyPackage : ToolkitPackage
{
    protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
    {
        await this.RegisterCommandsAsync();
        VS.Events.WindowEvents.ActiveFrameChanged += WindowEvents_ActiveFrameChanged;       
    }
}

问题是我的旧实现适用于这段代码:

[ProvideAutoLoad(UIContextGuids80.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
public sealed class MyPackage : ToolkitPackage
{
    protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
    {
        await this.RegisterCommandsAsync();
        // Getting `DTE2 dte` trough standard way...
        dte.Events.WindowEvents.WindowActivated += WindowEvents_WindowActivated;
    }
}

但是我不想在新的扩展版本中使用旧类型的代码,那么,在第一个实现示例中如何解决这个问题?

【问题讨论】:

    标签: visual-studio-extensions visual-studio-2022


    【解决方案1】:

    好吧,我不确定这个解决方案的“完美性”,但是在访问事件之前添加了这行代码 - 它可以工作。

    await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

    似乎您必须在主线程中才能访问这些事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      相关资源
      最近更新 更多