开发者

Visual Studio 2010 Extension - events not called

I trying to hook several Visual Studio events. Unfortunately I am failing in the first step. The event handlers are never called.

So my question is what I am doing wrong?

Here a little excerpt of my code.

// here are some attributes
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
public sealed class VSPackage : Package {
  EnvDTE80.DTE2 dte_;
  EnvDTE.DocumentEvents documentEvents_;
  EnvDTE.WindowEven开发者_如何学运维ts windowEvents_;

  public VSPackage2Package() {
    Trace.WriteLine("I am get called.");
  }

  protected override void Initialize() {
    Trace.WriteLine("I am get called too.");

    dte_ = (EnvDTE80.DTE2) System.Runtime.InteropServices.Marshal.
      GetActiveObject("VisualStudio.DTE.10.0");

    windowEvents_ = dte_.Events.WindowEvents;
    documentEvents_ = dte_.Events.DocumentEvents;

    windowEvents_.WindowCreated +=
      new EnvDTE._dispWindowEvents_WindowCreatedEventHandler(
        windowEvents_WindowCreated);

    documentEvents_.DocumentOpened +=
      new EnvDTE._dispDocumentEvents_DocumentOpenedEventHandler(
        documentEvents__DocumentOpened);

   Trace.WriteLine("Everything fine until here.");
  }

  void documentEvents__DocumentOpened(EnvDTE.Document document) {
    Trace.WriteLine("Never called");
  }

  void windowEvents_WindowCreated(EnvDTE.Window window) {
    Trace.WriteLine("Never called");
  }
}

Edit:

I get it working, looking at other sample code, I figured out that they sometimes getting the DTE object differently. Changing

    dte_ = (EnvDTE80.DTE2) System.Runtime.InteropServices.Marshal.
      GetActiveObject("VisualStudio.DTE.10.0");

to

    dte_ = GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;

and now everything is fine.


It should work.

I'm pretty sure that if you do the same from an Addin it would work. Packages can be painfull sometimes.

In fact, when a package is loaded the shell (DTE) may not be fully loaded yet. Try to register your events when it is.

To do so, use the OnShellPropertyChange event and the Zombie state to know when to register. http://social.msdn.microsoft.com/forums/en-US/vsx/thread/3097a0e1-68e3-47ea-a4ba-8511571b2487/

Read the following, I think it answers your question. Note : The GetService method is the same as calling GetGlobalService.

1. ServiceProvider.GlobalProvider

This new static property on the ServiceProvider class allows access to the global service provider from any code, as long as it is called from the main UI thread. This property is closely related to the Package.GetGlobalService static method which was available in previous versions of the MPF. The problem with Package.GetGlobalService was that it would fail if a package had not yet been initialized. This led to subtle ordering bugs in code that used the MPF libraries without initializing a package of their own. Sometimes they would work only because another package had already initialized the global ServiceProvider on their behalf. If that other package was uninstalled, or perhaps moved to a different version of the MPF, that static would no longer be initialized causing Package.GetGlobalService to fail.

Now, in MPF 10, you can call ServiceProvider.GlobalProvider at any time as long as you are calling from the UI thread. For compatibility, this mechanism will still use the ServiceProvider created by the first Package to be sited but, in the case where no Package has yet been initialized, MPF 10.0 now has the ability to obtain the global provider from the registered COM message filter. Package.GetGlobalService() is also hooked up to this new mechanism.


Make sure you are not boxing and unboxing your DTE object. I found this was the issue for me.

See my solution here: http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/eb1e8fd1-32ad-498c-98e9-25ee3da71004

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜