我正在尝试将使用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.
Frame identifier: ST:0:0:{deb760b0-be7a-4e88-9ef3-c09cfa7c16da}

Frame caption: ToolWindow1
Exception details:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation.
 at Microsoft.VisualStudio.Shell.Interop.IVsShell5.LoadPackageWithContext(Guid& packageGuid, Int32 reason, Guid& context)
 at Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.GetPackage()
 at Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.ConstructContent()
如果我取消注释InitializeAsync方法中的行await JoinableTaskFactory.SwitchToMainThreadAsync();
,则VS在FindToolWindow
上死锁。
那么,如何在InitializeAsync期间访问工具窗口?
如果我做不到,那么初始化后最早怎么办?
答案 0 :(得分:0)
不确定。但是,由于您想将Package迁移到AsyncPackage,因此我认为从AsyncPackage中查找FindToolWindowAsync可能更合适。
Package.FindToolWindow(Type, Int32, Boolean) Method
AsyncPackage.FindToolWindowAsync(Type, Int32, Boolean, CancellationToken) Method