1
votes

I have a difficulties with subscription to EventAggregator, complete vb.net code is below.

'EventSystem' MODULE - the simplified PRISM from Rachel's blog, turned to VB.net as module as follows:

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

I am able to 'Publish' to 'EventSystem' without problems, the code as follow where as message is used class 'NewMessage':

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

Difficulties with 'Subscription', code is below and unfortunately not works:

EventSystem.Subscribe(Of NewMessage)(AppNavigate)

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

Error: Argument not specified for parameter 'msg' of AppNavigate... This cannot understand, the class NewMessage has property msg. as below

Public Class NewMessage
    Public Msg As String
End Class

Please, help. Thank you

1

1 Answers

0
votes

Finally found solution for VB.NET, maybe will be useful for somebody. All above could be used to implement Simplified PRISM Event Aggregator, the 'Subscription' to be performed as follows using lambda expression:

 EventSystem.Subscribe(Of NewMessage)(AppNavigate)

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