简化PRISM事件聚合器-订阅错误



我在订阅EventAggregator时遇到了困难,下面是完整的vb.net代码。

"EventSystem"模块-Rachel博客中的简化PRISM,作为模块转到VB.net,如下所示:

Imports Prism.Events
Module EventSystem
Private _current As IEventAggregator
Public ReadOnly Property Current As IEventAggregator
Get
#Disable Warning BC40000 ' Type or member is obsolete
Return If(_current, (CSharpImpl.__Assign(_current, New EventAggregator())))
#Enable Warning BC40000 ' Type or member is obsolete
End Get
End Property
Private Function GetEvent(Of TEvent)() As PubSubEvent(Of TEvent)
Return Current.GetEvent(Of PubSubEvent(Of TEvent))()
End Function
Sub Publish(Of TEvent)()
Publish(Of TEvent)(Nothing)
End Sub
Sub Publish(Of TEvent)(ByVal [event] As TEvent)
GetEvent(Of TEvent)().Publish([event])
End Sub
Function Subscribe(Of TEvent)(ByVal action As Action, ByVal Optional threadOption As ThreadOption = ThreadOption.PublisherThread, ByVal Optional keepSubscriberReferenceAlive As Boolean = False) As SubscriptionToken
Return Subscribe(Of TEvent)(Sub(e) action(), threadOption, keepSubscriberReferenceAlive)
End Function
Function Subscribe(Of TEvent)(ByVal action As Action(Of TEvent), ByVal Optional threadOption As ThreadOption = ThreadOption.PublisherThread, ByVal Optional keepSubscriberReferenceAlive As Boolean = False, ByVal Optional filter As Predicate(Of TEvent) = Nothing) As SubscriptionToken
Return GetEvent(Of TEvent)().Subscribe(action, threadOption, keepSubscriberReferenceAlive, filter)
End Function
Sub Unsubscribe(Of TEvent)(ByVal token As SubscriptionToken)
GetEvent(Of TEvent)().Unsubscribe(token)
End Sub
Sub Unsubscribe(Of TEvent)(ByVal subscriber As Action(Of TEvent))
GetEvent(Of TEvent)().Unsubscribe(subscriber)
End Sub
Private Class CSharpImpl
<Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class
End Module

我能够毫无问题地"发布"到"EventSystem",代码如下所示,其中使用了as message类"NewMessage":

EventSystem.Publish(Of NewMessage)(New NewMessage With {.Msg = "Test"})

"订阅"的困难,代码如下,不幸的是不起作用:

EventSystem.Subscribe(Of NewMessage)(AppNavigate)
Private Sub AppNavigate(ByVal msg As NewMessage)
MsgBox(msg.Msg)
End Sub

错误:未为AppNavigate的参数"msg"指定参数。。。这无法理解,类NewMessage具有属性msg。如下

Public Class NewMessage
Public Msg As String
End Class

请帮帮我。谢谢

终于找到了VB.NET的解决方案,也许对某些人有用。以上所有内容都可用于实现简化PRISM事件聚合器,"订阅"将使用lambda表达式执行如下:

EventSystem.Subscribe(Of NewMessage)(AppNavigate)
Private ReadOnly AppNavigate As Action(Of NewMessage) = Sub(ByVal msg As NewMessage)
MsgBox(msg.Msg) 
End Sub

相关内容

  • 没有找到相关文章

最新更新