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

时间:2018-07-28 17:17:10

标签: prism subscribe eventaggregator

我在订阅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”而不会出现问题,使用消息类型为“ 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

请帮忙。谢谢

1 个答案:

答案 0 :(得分:0)

最后找到的VB.NET解决方案可能对某些人有用。以上所有内容均可用于实现简化的PRISM Event Aggregator,可使用lambda表达式按如下方式执行“订阅”:

 EventSystem.Subscribe(Of NewMessage)(AppNavigate)

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