I want ro register to the visual studio events which I can do with help of the IVsSolutionEvents Interface.
I also need to get the DTE2 Object which I can do with help of the IVsShellPropertyEvents.
But when I use both in my VsPackage the DTE2 object is not initialized.
VsPackage
//------------------------------------------------------------------------------
// <copyright file="NAntRunnerVSPackage.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using NAntRunner.Utils;
namespace NAntRunner
{
///<summary>
/// This is the class that implements the package exposed by this assembly.
/// </summary>
/// <remarks>
/// <para>
/// The minimum requirement for a class to be considered a valid package for Visual Studio
/// is to implement the IVsPackage interface and register itself with the shell.
/// This package uses the helper classes defined inside the Managed Package Framework (MPF)
/// to do it: it derives from the Package class that provides the implementation of the
/// IVsPackage interface and uses the registration attributes defined in the framework to
/// register itself and its components with the shell. These attributes tell the pkgdef creation
/// utility what data to put into .pkgdef file.
/// </para>
/// <para>
/// To get loaded into VS, the package must be referred by <Asset Type="Microsoft.VisualStudio.VsPackage" ...> in .vsixmanifest file.
/// </para>
/// </remarks>
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[Guid(PackageGuidString)]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideToolWindow(typeof(NAntRunnerToolWindow))]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
public sealed class NAntRunnerVSPackage : Package, IVsShellPropertyEvents
{
/// <summary>
/// NAntRunnerVSPackage GUID string.
/// </summary>
public const string PackageGuidString = "3c9d12d6-6cde-404c-b2bc-b22bde7b9cfe";
/// <summary>
/// Initializes a new instance of the <see cref="NAntRunnerVSPackage"/> class.
/// </summary>
public NAntRunnerVSPackage()
{
// Inside this method you can place any initialization code that does not require
// any Visual Studio service because at this point the package object is created but
// not sited yet inside Visual Studio environment. The place to do all the other
// initialization is the Initialize method.
}
#region Members
private IVsSolution _solution;
private SolutionEventHandler _solutionEventHandler;
private uint _solutionEventsCookie = 0;
private uint _cookie;
#endregion
#region Properties
public DTE Dte { get; set; }
public DTE2 Dte2 { get; set; }
#endregion
#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>
protected override void Initialize()
{
base.Initialize();
NAntRunnerToolWindowCommand.Initialize(this);
IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell;
if (shellService != null)
ErrorHandler.ThrowOnFailure(
shellService.AdviseShellPropertyChanges(this, out _cookie));
_solution = base.GetService(typeof(SVsSolution)) as IVsSolution;
// Solution Event Handling
if (_solution != null)
{
_solutionEventHandler = new SolutionEventHandler(this);
_solution.AdviseSolutionEvents(_solutionEventHandler, out _solutionEventsCookie);
}
}
protected override void Dispose(bool disposing)
{
if (_solutionEventsCookie != 0)
{
_solution.UnadviseSolutionEvents(_solutionEventsCookie);
_solutionEventsCookie = 0;
}
_solutionEventHandler = null;
base.Dispose(disposing);
}
public int OnShellPropertyChange(int propid, object var)
{
// when zombie state changes to false, finish package initialization
if ((int)__VSSPROPID.VSSPROPID_Zombie == propid)
{
if ((bool)var == false)
{
Dte = GetService(typeof(SDTE)) as DTE;
Dte2 = GetService(typeof(SDTE)) as DTE2;
IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell;
if (shellService != null)
ErrorHandler.ThrowOnFailure(
shellService.UnadviseShellPropertyChanges(this._cookie));
this._cookie = 0;
}
}
return VSConstants.S_OK;
}
#endregion
#region Solution Event Handling
internal void HandleSolutionEvent(string eventName)
{
Debug.WriteLine(eventName);
}
#endregion
}
}
SolutionEventsHandler
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
namespace NAntRunner.Utils
{
public class SolutionEventHandler : IVsSolutionEvents
{
#region Members
private NAntRunnerVSPackage _package;
#endregion
#region Construktor
public SolutionEventHandler(NAntRunnerVSPackage package)
{
_package = package;
}
#endregion
#region IVsSolutionEvents
public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
{
_package.HandleSolutionEvent("OnAfterOpenProject");
return VSConstants.S_OK;
}
public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
{
_package.HandleSolutionEvent("OnQueryCloseProject");
return VSConstants.S_OK;
}
public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
{
_package.HandleSolutionEvent("OnBeforeCloseProject");
return VSConstants.S_OK;
}
public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
{
_package.HandleSolutionEvent("OnAfterLoadProject");
return VSConstants.S_OK;
}
public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
{
_package.HandleSolutionEvent("OnQueryUnloadProject");
return VSConstants.S_OK;
}
public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
{
_package.HandleSolutionEvent("OnBeforeUnloadProject");
return VSConstants.S_OK;
}
public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
{
_package.HandleSolutionEvent("OnAfterOpenSolution");
return VSConstants.S_OK;
}
public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
{
_package.HandleSolutionEvent("OnQueryCloseSolution");
return VSConstants.S_OK;
}
public int OnBeforeCloseSolution(object pUnkReserved)
{
_package.HandleSolutionEvent("OnBeforeCloseSolution");
return VSConstants.S_OK;
}
public int OnAfterCloseSolution(object pUnkReserved)
{
_package.HandleSolutionEvent("OnAfterCloseSolution");
return VSConstants.S_OK;
}
#endregion
}
}
Any Idea how to use both?
regards,
Marko