--- lang: ja-jp breaks: true --- # `VSIX` で、 ソリューションが開かれたイベントを取得する方法 2022-01-26 ```csharp= [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] [Guid(VSIXProject19Package.PackageGuidString)] public sealed class VSIXProject19Package : AsyncPackage { /// <summary> /// VSIXProject19Package GUID string. /// </summary> public const string PackageGuidString = "08490e99-c061-4fa0-88d0-00684788af8c"; #region Package Members /// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> /// <param name="cancellationToken">A cancellation token to monitor for initialization cancellation, which can occur when VS is shutting down.</param> /// <param name="progress">A provider for progress updates.</param> /// <returns>A task representing the async work of package initialization, or an already completed task if there is none. Do not return null from this method.</returns> protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress) { // When initialized asynchronously, the current thread may be a background thread at this point. // Do any initialization that requires the UI thread after switching to the UI thread. //await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); bool isSolutionLoaded = await IsSolutionLoadedAsync(cancellationToken); if (isSolutionLoaded) { HandleOpenSolution(); } // Listen for subsequent solution events SolutionEvents.OnAfterOpenSolution += HandleOpenSolution; SolutionEvents.OnAfterLoadProject += SolutionEvents_OnAfterLoadProject; SolutionEvents.OnAfterBackgroundSolutionLoadComplete += SolutionEvents_OnAfterBackgroundSolutionLoadComplete; SolutionEvents.OnAfterLoadAllDeferredProjects += SolutionEvents_OnAfterLoadAllDeferredProjects; } #endregion private void SolutionEvents_OnAfterLoadAllDeferredProjects(object sender, EventArgs e) { Debug.WriteLine($"SolutionEvents_OnAfterLoadAllDeferredProjects"); } private void SolutionEvents_OnAfterBackgroundSolutionLoadComplete(object sender, EventArgs e) { Debug.WriteLine($"SolutionEvents_OnAfterBackgroundSolutionLoadComplete"); } private void SolutionEvents_OnAfterLoadProject(object sender, LoadProjectEventArgs e) { Debug.WriteLine($"SolutionEvents_OnAfterLoadProject {e.ToString()}"); } private async Task<bool> IsSolutionLoadedAsync(CancellationToken cancellationToken) { await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); var solService = await GetServiceAsync(typeof(SVsSolution)) as IVsSolution; ErrorHandler.ThrowOnFailure(solService.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out object value)); return value is bool isSolOpen && isSolOpen; } private void HandleOpenSolution(object sender = null, EventArgs e = null) { // Handle the open solution and try to do as much work // on a background thread as possible Debug.WriteLine($"HandleOpenSolution"); } } ``` ###### tags: `VSIX` `AsyncPackage` `SolutionEvents`