简体   繁体   English

如何在异步加载初始解决方案时通知 VSPackage

[英]How to have a VSPackage notified when initial solution loaded asynchronously

This question deals with my question, just not with changes made in Visual Studio 2019 , where VSIX projects load asynchronously, where in previous versions of Visual Studio VSIX projects loaded synchronously. 这个问题涉及我的问题,而不是在Visual Studio 2019中所做的更改,其中VSIX项目异步加载,而以前版本的 Visual Studio VSIX 项目同步加载。

I am using IVsSolutionEvents3 in my VSIX projects to get notified of我在我的 VSIX 项目中使用IVsSolutionEvents3来获取通知

VSTestPackage1: OnAfterOpenProject
VSTestPackage1: OnQueryCloseSolution
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseSolution
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseProject
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseProject
VSTestPackage1: OnAfterCloseSolution

I modified the decoration to my VSIX project to incorporate the recommended SolutionExistsAndFullyLoaded , however the I do not get any notifications for the very first solution that a user opens.我修改了我的 VSIX 项目的装饰,以合并推荐的SolutionExistsAndFullyLoaded ,但是我没有收到用户打开的第一个解决方案的任何通知。 In VS2019 that is from there new dialog that shows prior to the actual IDE.在 VS2019 中,新对话框显示在实际 IDE 之前。

[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExistsAndFullyLoaded_string, PackageAutoLoadFlags.BackgroundLoad)]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[Guid(VSTestPackage1.PackageGuidString)]
[InstalledProductRegistration("#110", "#112", "2017.1.4.664", IconResourceID = 400)]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
[ProvideMenuResource("Menus.ctmenu", 1)]
public sealed class VSTestPackage1 :
    AsyncPackage,
    IVsSolutionEvents3,
    IVsUpdateSolutionEvents,
    IVsBuildStatusCallback,
    IDisposable
{
}

I receive notifications for all solutions after the initial solution loads and for the closing of the initial solution, just not the loading of the initial solution.在初始解决方案加载和初始解决方案关闭后,我会收到所有解决方案的通知,而不是初始解决方案的加载。 What I see from debugging is that after the initial solution loads, my VSIX projects initializes.我从调试中看到的是,在初始解决方案加载后,我的 VSIX 项目初始化。

protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
     System.Diagnostics.Debug.WriteLine("VSTestPackage1: Entering Initialize()");
     ...
}

The InitializeAsync() call does not provide a IVsHierarchy pHierarchy , which would allow me to extract the already open solution/project. InitializeAsync()调用不提供IVsHierarchy pHierarchy ,这将允许我提取已经打开的解决方案/项目。

How can I get notified of the initial solution/project?如何获得初始解决方案/项目的通知?

Simple answer is you cannot, as your package is now being loaded asynchronously, so the solution may have already been loaded, and fired those events, before your package has the opportunity to sink/advise on those event interfaces.简单的答案是您不能,因为您的 package 现在正在异步加载,因此在您的 package 有机会在这些事件接口上接收/建议之前,解决方案可能已经加载并触发了这些事件。 You'll need change up the logic in your package to accommodate this possibility.您需要更改 package 中的逻辑以适应这种可能性。

Basically, you'll need to see if a solution or project you are interested in is already loaded, and process it accordingly.基本上,您需要查看您感兴趣的解决方案或项目是否已加载,并相应地进行处理。

Mad's posted a blog describing the impact on the move to async package loading in the following in one of his earlier blog posts: Mad 在他之前的一篇博文中发布了一篇博客,描述了迁移到异步 package 加载的影响:

Improving the responsiveness of critical scenarios by updating auto load behavior for extensions 通过更新扩展的自动加载行为来提高关键场景的响应能力

In particular the section "Impact on package implementations".特别是“对 package 实现的影响”部分。

Sincerely,真挚地,

I found that if you include a ToolWindow in your VSIX and this ToolWindow is in the configuration package.cs like,我发现如果您在 VSIX 中包含一个 ToolWindow 并且此 ToolWindow 位于配置 package.cs 中,例如,

[ProvideToolWindow(typeof(ToolWindow1))] [提供工具窗口(typeof(ToolWindow1))]

.. and suppose your ToolWindow1 is shown via some Menu View/Other Windows, you have properly sized it and left it on screen when closing Visual Studio . ..并假设您的 ToolWindow1 通过某些菜单视图/其他 Windows 显示,您已正确调整大小并在关闭 Visual Studio 时将其留在屏幕上 After that, start Visual Studio again and load a solution.. The Toolwindow will show very quickly, and the loading of the ToolWindow will preceed the loading of the first Solution !之后,再次启动 Visual Studio 并加载解决方案。工具窗口将很快显示,并且工具窗口的加载将于第一个解决方案的加载!

On loading its UI, Visual Studio will show the ToolWindow before the solution is loaded, so the constructor of the ToolWindow can set your event handlers.在加载其 UI 时,Visual Studio 将在加载解决方案之前显示 ToolWindow,因此 ToolWindow 的构造函数可以设置您的事件处理程序。 Doing so, you can handle the solution load as usual, through receiving your events and processing them.这样做,您可以像往常一样通过接收事件并处理它们来处理解决方案负载。

I admit this is a trick, not a real solution.我承认这是一个技巧,而不是真正的解决方案。 When the user chooses to NOT display your ToolWindow, you will end up with the initialisation half way.当用户选择不显示您的 ToolWindow 时,您将在初始化过程中结束。 When MS decides to change the order of events in a future release, your VSIX must be changed.当 MS 决定在未来版本中更改事件顺序时,您的 VSIX 必须更改。 But at least this gives the option to run an existing VSIX in async package mode, loading the first solution properly.但至少这提供了在异步 package 模式下运行现有 VSIX 的选项,正确加载第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM