I am having trouble even getting the InitalizeAysnc method to even run, break points are not getting hit. I assume this is why my custom code for BeforeExecute isn't running.
I have searched google and VSIX github repos to no end and can't figure this out.
Here is my latest attempt:
> global using Community.VisualStudio.Toolkit;
> global using Microsoft.VisualStudio.Shell;
> global using System;
> global using Task = System.Threading.Tasks.Task;
> using EnvDTE;
> using EnvDTE80;
> using Microsoft;
> using System.Diagnostics;
> using System.IO;
> using System.Runtime.InteropServices;
> using System.Threading;
>
> namespace VSIXProject13
> {
> [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
> [InstalledProductRegistration(Vsix.Name, Vsix.Description, Vsix.Version)]
> [ProvideMenuResource("Menus.ctmenu", 1)]
> [Guid(PackageGuids.VSIXProject13String)]
> public sealed class VSIXProject13Package : ToolkitPackage
> {
>
> public const string PackageGuidString = "bcf67d7a-8e10-4fe5-af47-2fa32560656a";
> private DTE2 _dte;
>
>
> protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
> {
>
> await this.RegisterCommandsAsync();
>
> await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
>
> await base.InitializeAsync(cancellationToken, progress);
>
> _dte = (DTE2)await GetServiceAsync(typeof(DTE));
> Assumes.Present(_dte);
>
> // Hook up event handlers for before and after command execution
> _dte.Events.CommandEvents.BeforeExecute += OnBeforeCommandExecute;
> _dte.Events.CommandEvents.AfterExecute += OnAfterCommandExecute; _dte = (DTE2)GetService(typeof(DTE));
>
>
>
> }
>
>
> private void OnBeforeCommandExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
> {
> var sw = new StreamWriter(@"c:\test\vsix.log");
> sw.AutoFlush = true;
> sw.WriteLine("here-1\n");
> sw.Close();
> // Add custom code to run before the built-in command execution
> // Example: Console.WriteLine("Before executing command: " + _dte.Commands.Item(Guid, ID).Name);
> }
>
> /// <summary>
> /// Event handler for after command execution.
> /// </summary>
> private void OnAfterCommandExecute(string Guid, int ID, object CustomIn, object CustomOut)
> {
> // Add custom code to run after the built-in command execution
> // Example: Console.WriteLine("After executing command: " + _dte.Commands.Item(Guid, ID).Name);
> Debug.Print("We are in the OnAfterCommandExecute event");
> }
>
> /// <summary>
> /// Event handler for when the IDE begins to shut down.
> /// </summary>
> private void OnBeginShutdown()
> {
> // Clean up resources and unregister event handlers
> _dte.Events.DTEEvents.OnBeginShutdown -= OnBeginShutdown;
> _dte.Events.CommandEvents.BeforeExecute -= OnBeforeCommandExecute;
> _dte.Events.CommandEvents.AfterExecute -= OnAfterCommandExecute;
> }
> }
> }
>
> ```