NotifyPropertyChangedRecipients - IridiumIO/MVVM.VBSourceGenerators GitHub Wiki

Fields decorated with this will have a Broadcast call added to the generated property. This can be listened for in any other viewmodel utilising the Messenger system in the MVVM Toolkit.

Microsoft Docs


Supported on

  • Fields
  • Properties (VB.NET does not support partial properties)
  • Classes

Requires

  • Class must be partial
  • Class must inherit from ObservableRecipient
  • Class-level attribute is not supported yet

Usage

Given the decorated field:

Public Partial Class MainViewModel : Inherits ObservableRecipient

    <ObservableProperty>
    <NotifyPropertyChangedRecipients>
    Private _firstName As String

End Class

The following will be generated (note: Base ObservableProperty generated code has been omitted for brevity)

Public Property FirstName As String
    Get
        Return _firstName
    End Get
    Set(value As String)
        Broadcast(_oldValue, value, "FirstName")
    End Set
End Property

And can be used like so in a complete example:

Public Partial Class MainViewModel : Inherits ObservableRecipient

    <ObservableProperty>
    <NotifyPropertyChangedRecipients>
    Private _firstName As String

End Class

Public Partial Class RecipientViewModel : Inherits ObservableRecipient
    Implements IRecipient(Of PropertyChangedMessage(Of MainViewModel))


    Public Sub Receive(message As PropertyChangedMessage(Of MainViewModel)) Implements IRecipient(Of PropertyChangedMessage(Of MainViewModel)).Receive

        Dim sender = TryCast(message.Sender, MainWindowViewModel)

        If message.PropertyName = NameOf(sender.FirstName) Then
            Debug.WriteLine($"FirstName in MainViewModel changed to: {message.NewValue}")
        End If

    End Sub
End Class