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

问题描述 投票:1回答:1

我正在尝试将使用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期间访问工具窗口?

如果我不能,我怎么能在初始化后最早做到这一点?

visual-studio-extensions vsix
1个回答
0
投票

不确定。但是既然你想将Package迁移到AsyncPackage,我认为来自AsyncPackage的FindToolWindowAsync可能更合适。

Package.FindToolWindow(Type, Int32, Boolean) Method

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

© www.soinside.com 2019 - 2024. All rights reserved.