Overview:
Visual Studio integration by deriving from base class 'AsyncPackage'
Base class "AsyncPackage" implements the required interface IVsPackage which cares for proper integration with Visual Studio:
public sealed class TestExecWindowPackage : AsyncPackage
Getting acess to DTE
Microsoft Visual Studio allows programmatic access via a special environment, the "DTE" (= development tools environment). The "DTE object" is the root of the automation model for Visual Studio.
To get access to DTE object you can establish a connection within the package class of your C# assembly. The reference to the DTE object can be distributed within your application classes as needed.
First you have to define appropriate data:
private EnvDTE80.DTE2 dte;
private DteInitializer dteInitializer;
Request for the DTE object is done within the custom method InitializeDTE:
private void InitializeDTE()
{
Debug.WriteLine("InitializeDTE-Begin");
this.dte = this.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE))
as EnvDTE80.DTE2;
Executor.dte = this.dte;
VisualStudioConnector.dte = this.dte;
if (this.dte == null)
{
Debug.WriteLine("InitializeDTE: DTE is not yet fully initialized");
IVsShell shellService;
shellService = this.GetService(typeof(SVsShell)) as IVsShell;
this.dteInitializer = new DteInitializer(shellService, this.InitializeDTE);
}
else
{
Debug.WriteLine("InitializeDTE: DTE is set");
this.dteInitializer = null;
}
Debug.WriteLine("InitializeDTE-End");
}
The call of InitializeDTE is done within overridden base method InitializeAsync:
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData> progress)
{
await Task.Delay(5000);
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
TestExecWindowCommand.Initialize(this);
base.Initialize();
InitializeDTE();
}