首先,这是我在这个社区中的第一个问题。如果我做错了方法,请给我一些建议。
我需要一点帮助。我实际上是在BMEcat类库上工作,BMEcat是电子目录的数据交换格式。一切正常,但是我意识到处理非常大的文件时存在内存问题。因此,我想为任何已处理的商品/产品发送事件,而不是在内存中创建巨大的结构。 这是我的问题开始的点。
我有一个类CTRANSACTION
,从中派生了类CT_NEW_CATALOG
,CT_UPDATE_PRODUCTS
和CT_UPDATE_PRICES
。
在基类CTRANSACTION
中定义了一个事件:
Public Event Transaction_OnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
类CBMECAT
具有变量
Public WithEvents TRANSACTION As CTRANSACTION
和事件处理程序
Private Sub TRANSACTION_Transaction_OnNewArticle(sender As Object, e As ArticleEventArgs) Handles TRANSACTION.Transaction_OnNewArticle
'...
End Sub
因为我无法从派生的Transaction_OnNewArticle
类发送事件CT_NEW_CATALOG
,所以我让它调用TransactionEventOnNewArticle
中定义的CTRANSACTION
方法。 TransactionEventOnNewArticle
然后呼叫RaiseEvent Transaction_OnNewArticle
。
一切正常,但事件Transaction_OnNewArticle
并未触发。有办法解决吗?
Public MustInherit Class CTRANSACTION
Inherits CBMECAT_NODE
Public Event Transaction_OnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
Public Sub TransactionEventOnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
RaiseEvent Transaction_OnNewArticle(sender, e)
End Sub
Public Class CT_NEW_CATALOG
Inherits CTRANSACTION
Public Overrides Sub EventOnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
TransactionEventOnNewArticle(sender, e)
End Sub
Public Class CBMECAT
Inherits CBMECAT_NODE
Public WithEvents TRANSACTION As CTRANSACTION
Private Sub TRANSACTION_Transaction_OnNewArticle(sender As Object, e As ArticleEventArgs) Handles TRANSACTION.Transaction_OnNewArticle
'THIS method is never called - why?
End Sub
End Class
更新
Public Class CBMECAT_ELEMENT
Public Overridable Sub EventOnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
End Sub
'Please notice that CBMECAT_ELEMENT is the base class of EVERY other class in the library.
'There is a class CBMECAT_NODE, which represents every node of the BMEcat XML structure and is derived from CBMECAT_ELEMENT.
'In CBMECAT_NODE is EventOnNewArticle called whenever an article is processed;
Public Class CBMECAT_NODE
Inherits CBMECAT_ELEMENT
Public Overridable Function CreateChildNode(ByRef Nodename As String, Optional ByRef Parent As CBMECAT_NODE = Nothing) As CBMECAT_ELEMENT
Select Case Nodename
[..]
Case ELEMENT_ARTICLE
CreateChildNode = New CARTICLE(Parent)
Dim e As New ArticleEventArgs With
{
.ARTICLE = CreateChildNode
}
EventOnNewArticle(Me, e)
[..]
更新
Public Class CARTICLE
Inherits CBMECAT_NODE
Public Sub New(ByRef Father As CBMECAT_NODE)
[..]
Public Overrides Sub Read()
[..]
Public Overrides Sub Write()
[..]
Public Overrides Sub Validate()
[..]
更新
呼叫顺序:
CBMECAT_NODE.CreateChildNode调用CT_NEW_CATALOG.EventOnNewArticle <-确定
CT_NEW_CATALOG.EventOnNewArticle调用CTRANSACTION.TransactionEventOnNewArticle <-确定
CTRANSACTION.TransactionEventOnNewArticle触发事件Transaction_OnNewArticle 但事件处理者在CBMECAT中未收到该事件
如果我从CTRANSACTION中的方法手动触发事件,则事件被事件处理程序接收。
我还尝试了AddHandler / RemoveHandler,但这也行不通。
答案 0 :(得分:0)
Visual Vincent,谢谢您帮助我解决问题。实际上,这有点“复杂”。
CBMECAT类具有以下读取方法:
Public Overrides Sub Read()
MyBase.Read()
GetContent(HEADER, ELEMENT_HEADER)
Select Case TransactionType
Case TransactionTypes.T_NEW_CATALOG
GetContent(TRANSACTION, ELEMENT_T_NEW_CATALOG)
Case TransactionTypes.T_UPDATE_PRICES
GetContent(TRANSACTION, ELEMENT_T_UPDATE_PRICES)
Case TransactionTypes.T_UPDATE_PRODUCTS
GetContent(TRANSACTION, ELEMENT_T_UPDATE_PRODUCTS)
Case Else
ReportError(ERROR_BMECAT_UNKNOWN_TRANSACTION_TYPE)
End Select
Validate()
End Sub
MyBase.Read读取完整的XML文件,并且在读取时应触发事件。但是目前,变量 TRANSACTION 尚未通过其值分配。这是通过在读取过程完成后调用GetContent来完成的。
我已更改为:
TRANSACTION = New CT_NEW_CATALOG
TRANSACTION.Read()
现在所有事件均按预期触发。 我将从 CBMECAT_ELEMENT 中删除 TransactionEventOnNewArticle()。再次感谢Vincent,您的建议。 :-)