简体   繁体   English

与vb.net表单事件相关的“无法使用与其基础RCW分离的COM对象”错误

[英]“COM object that has been separated from its underlying RCW cannot be used” error related to vb.net form event

I'm hooking an arcobjects map event to a vb.net form to listen for map selection changes. 我将arcobjects地图事件挂接到vb.net表单上,以侦听地图选择更改。 This all works fine but users are reporting this error occassionally when opening the form. 一切正常,但是用户在打开表单时偶尔会报告此错误。 I can't see any pattern to reproduce the error and it seems to be random. 我看不到任何模式来重现该错误,而且似乎是随机的。

"COM object that has been separated from its underlying RCW cannot be used" “无法使用与其基础RCW分离的COM对象”

It originates from the form Load() method where I am hooking the event. 它源自我钩住事件的表单Load()方法。

Can anyone help me understand what I've done wrong? 谁能帮助我了解我做错了什么? I'm unhooking the map selection event in the FormClosing() event which I think is the correct approach. 我正在取消FormClosing()事件中的地图选择事件,我认为这是正确的方法。

Public Class MyForm

    Private _activeViewEvents As IActiveViewEvents_Event

    Private Sub FormLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _activeViewEvents = TryCast(pMxDoc.ActiveView.FocusMap, IActiveViewEvents_Event)
        AddHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged    
    End Sub

    Private Sub SelectionChanged
        'do something when selection is changed
    End Sub

    Private Sub FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        RemoveHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
    End Sub

End Class

The approach you are taking to creating and destroying your handlers are valid. 您正在使用的创建和销毁处理程序的方法是有效的。 You can receive a RCW COM Exception when the map document is changed while your form is open. 打开表单时更改地图文档时,您会收到RCW COM异常。 Since you are using the FocusMap to create the handles, when the document is changed, so is the FocusMap , which means you need to re-create your handlers for the new map document. 由于您正在使用FocusMap创建句柄,因此在更改文档时, FocusMap也是FocusMap ,这意味着您需要为新地图文档重新创建处理程序。

Ok so I think i've resolved this via use of the ActiveViewChanged event. 好的,我想我已经通过使用ActiveViewChanged事件解决了这个问题。 Instead of rehooking the event on each form load or new document event, I tried listening for when the ActiveViewChanged event was fired and rehooking the SelectionChanged event each time. 我尝试重新监听ActiveViewChanged事件的触发时间,并每次重新选择SelectionChanged事件,而不是在每次加载表单或新文档事件时重新捕获事件。 Turns out this is fired more than once each time a new document is opened (not sure why). 事实证明,每次打开新文档时都会触发多次(不确定原因)。 Anyway, problem seems to have gone. 无论如何,问题似乎已经消失了。 Here's some example code: 这是一些示例代码:

Public Class MyForm

Private _activeViewEvents As IActiveViewEvents_Event
Private _docEvents As IDocumentEvents_Event

Private Sub FormLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler _docEvents.ActiveViewChanged, AddressOf ActiveViewChanged
End Sub

Private Sub ActiveViewChanged()
    Dim maps = pMxDoc.Maps
    For i = 0 to maps.Count - 1 'remove handlers from all maps
        RemoveActiveViewEvents(maps.Item(i))
    Next
    SetupActiveViewEvent(pMxDoc.ActiveView.FocusMap) 'only add handler to active map
End Sub

Private Sub RemoveActiveViewEvents(map As IMap)
    _activeViewEvents = CType(map, IActiveViewEvents_Event)
    RemoveHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
End Sub

Private Sub SetupActiveViewEvents(map As IMap)
    _activeViewEvents = CType(map, IActiveViewEvents_Event)
    AddHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
End Sub

Private Sub SelectionChanged
    'do something when selection is changed
End Sub

End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与其基础RCW分开的Infopath COM对象无法使用 - Infopath COM object that has been separated from its underlying RCW cannot be used VB.NET跟踪COM RCW错误 - VB.NET Track COM RCW error 有没有办法在VB.NET中知道是否为事件注册了处理程序? - Is there a way to know in VB.NET if a handler has been registered for an event? VB.net,无法访问已添加到项目的 OpenFileDialog - VB.net, cannot access OpenFileDialog that has been added to project 如何修复VB.net中与该事件相关的错误(Windows Phone 7) - How to fix this event related error in VB.net (windows phone 7) VB.NET错误:“ ConnectionString属性尚未初始化。” - VB.NET Error: “The ConnectionString property has not been initialized.” “VBS中的ConnectionString属性尚未初始化”错误 - “The ConnectionString property has not been initialized” error in VB.NET VB.NET的ConnectionString属性尚未初始化错误 - The ConnectionString property has not been initialized error with VB.NET VB.NET 中的“ConnectionString 属性尚未初始化”错误 - “The ConnectionString property has not been initialized” error in VB.NET 尝试使用 vb.net 管理 Excel 但我收到“无法转换 COM ZA8CFDE6331149EB2AC96F 类型…”来自8 - Trying to manage Excel with vb.net but i recived “Cannot convert COM object from type…”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM