Question

i'm currently experimenting wpf communication between two views of different modules using prism IEventAggregator. publishing module and subscribing module is working fine, for some reason i can't understand why the subscriber UI is not updating.i place a button to display a msgbox at the subscriber module just to be sure that it receives it and it does. and i think i properly implement the INotifyPropertyChanged.

if i place subscribe in subscriber view's code-behind it works as i want it to be....do i do it the wrong way? please correct me. thanks.

a separate class for module message passing. this class is from this post http://www.shujaat.net/2010/12/wpf-eventaggregator-in-prism-40-cal.html

Public Class SendServices
Public Shared Property SendMessage As EventAggregator

Shared Sub New()
    SendMessage = New EventAggregator
End Sub
End Class

Publisher :

Public Class Module1ViewModel

Private _msgsend As String
Public WriteOnly Property MessageSend As String
    Set(value As String)
        _msgsend = value
    End Set
End Property

Public Sub Send()
    SendServices.SendMessage.GetEvent(Of SendStringEvent).Publish(New SendString With {.Name = _msgsend})
End Sub
End Class

Subscriber :

Public Class Module2ViewModel
Implements INotifyPropertyChanged

Private _receivedMSG As String
Public Property ReceivedMSG As String
    Get
        Return _receivedMSG
    End Get
    Set(value As String)
        _receivedMSG = value

        OnPropertyChanged("ReceivedMSG")
    End Set
End Property
'Binded to subscriber View button using interactions
Public Sub Received()
    MsgBox(ReceivedMSG)
End Sub

Private Sub ReceivedMessage(msg As SendString)
    _receivedMSG = msg.Name
End Sub

Public Sub New()
    SendServices.SendMessage.GetEvent(Of SendStringEvent)().Subscribe(AddressOf ReceivedMessage, ThreadOption.UIThread, False)
End Sub

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(ByVal name As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class

Subscriber View code-behind

Public Class Module2View
    Sub New()
        InitializeComponent()
        Me.DataContext = New Module2ViewModel
    End Sub
End Class

and the binding part to display the message

<TextBox Height="23" HorizontalAlignment="Left" Margin="111,39,0,0" Name="TextBox1" VerticalAlignment="Top" Width="158" Text="{Binding Path=ReceviedMSG}"/>
Was it helpful?

Solution

You don't fire the OnPropertyChanged event because you are directly setting the field _receivedMSG in your handler which is bypassing the property setter which fires the event.

So you should use the property setter instead:

Private Sub ReceivedMessage(msg As SendString)
    ReceivedMSG = msg.Name
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top