visual studio shell SelectionEvents OnChange事件丢失问题

本文探讨了 Visual Studio 中 COM 插件的事件订阅问题。详细解释了为何某些情况下事件不会被触发,并提供了相应的解决方案,包括如何通过维护对事件类的引用以确保事件能够正常工作。

解决办法如下:

http://stackoverflow.com/questions/14165885/add-in-events-are-never-executed



If you look at applicationObject in the debugger you'll see its a COM object, but the xxxEvents classes are not (If you can't get the code to break on OnConnection, then possibly your addin isn't getting loaded when you debug, check the tools menu)

Events in COM are handled by a separate COM interface (several in this case) in the other direction which the server (VS) calls to fire them.

Although COM objects have a similar typed assembly concept to CLR assemblies they are unmanaged code internally, so cannot hold roots to managed objects.

So although you can hook a delegate to a COM event in a way that looks exactly like a native CLR event, your event is hooked to an RCW (runtime callable wrapper). There is a COM reference from the server to the RCW com interface, but without a CLR root the RCW eventually gets disposed which unloads the com interface, after which you won't sink any events.

I'm not sure but I think this normally works when you consume a single COM object with a direct association to its event interfaces so its maybe due to how the DTE COM interface is structured...

Anyway as others have said you just need any kind of managed reference to the BuildEvents, SelectionEvents and DocumentEvents classes from the app object to fix it. Multiple instances of VS load separate instances of the add in so you could just add a list of static object refs and set them in OnConnect.

Subscription to DTE events doesn't seem to work - Events don't get called

http://msdn.microsoft.com/en-us/library/k639e386.aspx


If you look at applicationObject in the debugger you'll see its a COM object, but the xxxEvents classes are not (If you can't get the code to break on OnConnection, then possibly your addin isn't getting loaded when you debug, check the tools menu)

Events in COM are handled by a separate COM interface (several in this case) in the other direction which the server (VS) calls to fire them.

Although COM objects have a similar typed assembly concept to CLR assemblies they are unmanaged code internally, so cannot hold roots to managed objects.

So although you can hook a delegate to a COM event in a way that looks exactly like a native CLR event, your event is hooked to an RCW (runtime callable wrapper). There is a COM reference from the server to the RCW com interface, but without a CLR root the RCW eventually gets disposed which unloads the com interface, after which you won't sink any events.

I'm not sure but I think this normally works when you consume a single COM object with a direct association to its event interfaces so its maybe due to how the DTE COM interface is structured...

Anyway as others have said you just need any kind of managed reference to the BuildEvents, SelectionEvents and DocumentEvents classes from the app object to fix it. Multiple instances of VS load separate instances of the add in so you could just add a list of static object refs and set them in OnConnect.

Subscription to DTE events doesn't seem to work - Events don't get called

http://msdn.microsoft.com/en-us/library/k639e386.aspx

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值