在InitializeAsync期间在2015 VSIX AsyncPackage中使用FindToolWindow

时间:2019-03-10 02:22:51

标签: visual-studio-extensions vsix

我正在尝试将使用Package的vsix迁移到AsyncPackage。

问题在于,最初我的Package中的Initialize覆盖方法使用

ToolWindowPane pane = FindToolWindow(typeof(ToolWindow1), 0, true);

转到工具窗口内的UserControl。

但是在更新为使用AsyncPackage之后,例如

[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideToolWindow(typeof(ToolWindow1))]
[Microsoft.VisualStudio.Shell.ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
[Guid(ToolWindow1Package.PackageGuidString)]
[ProvideToolWindowVisibility(typeof(ToolWindow1), /*UICONTEXT_SolutionExists*/"f1536ef8-92ec-443c-9ed7-fdadf150da82")]

public sealed class ToolWindow1Package : AsyncPackage


        protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
        {

            //await JoinableTaskFactory.SwitchToMainThreadAsync();
            ToolWindow1Command.Initialize(this, JoinableTaskFactory);

            ToolWindowPane pane = FindToolWindow(typeof(ToolWindow1), 0, true);
             await base.InitializeAsync(cancellationToken, progress);
            return;

        }

FindToolWindow的调用失败,在ActivityLog.xml中调用

Construction of frame content failed.&#x000D;&#x000A;Frame identifier: ST:0:0:{deb760b0-be7a-4e88-9ef3-c09cfa7c16da}&#x000D;&#x000A;
Frame caption: ToolWindow1&#x000D;&#x000A;Exception details:&#x000D;&#x000A;System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation.&#x000D;&#x000A;   at Microsoft.VisualStudio.Shell.Interop.IVsShell5.LoadPackageWithContext(Guid&amp; packageGuid, Int32 reason, Guid&amp; context)&#x000D;&#x000A;   at Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.GetPackage()&#x000D;&#x000A;   at Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.ConstructContent()

如果我取消注释InitializeAsync方法中的行await JoinableTaskFactory.SwitchToMainThreadAsync();,则VS在FindToolWindow上死锁。

那么,如何在InitializeAsync期间访问工具窗口?

如果我做不到,那么初始化后最早怎么办?

1 个答案:

答案 0 :(得分:0)

不确定。但是,由于您想将Package迁移到AsyncPackage,因此我认为从AsyncPackage中查找FindToolWindowAsync可能更合适。

Package.FindToolWindow(Type, Int32, Boolean) Method

AsyncPackage.FindToolWindowAsync(Type, Int32, Boolean, CancellationToken) Method