dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Messenger keeps stacking messages even it is unregistered

Open JawadJaber opened this issue 8 months ago • 1 comments

Describe the bug

I have come through this bug and did not find a solution for it, even AI was unable to answer me. So, I think it is a bug in the library. I have created a sample repo as well as a video showing the behaviour. The issue in short, is that when you register StrongReferenceMessenger and then unregister it, when you open it again previous messengers registrations will be invoked and stacking messages are in place.

Here is the repo to reproduce

https://github.com/JawadJaber/WpfTestStrongReferenceMessengerStackingIssue

Regression

No response

Steps to reproduce

https://github.com/user-attachments/assets/d0d69615-8445-46c2-a935-b4985683655a

Expected behavior

When creating new window twice, the logger should receive only one line message not two.

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [x] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.4.0

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

JawadJaber avatar Jul 03 '25 12:07 JawadJaber

// First: always unregister to avoid stacking
StrongReferenceMessenger.Default.Unregister<CoreUserControl, string>(control, "Check Uncheck Design User Control");

// Register again
var i = 0;
StrongReferenceMessenger.Default.Register<CoreUserControl, string>(control, (a, p) =>
{
    if (p == "Check Uncheck Design User Control")
    {
        control.MessagesListBox.Items.Add($"Check Uncheck Design User Control + {i}");
        i++;
        Debug.WriteLine($"New message with i {i}");
        this.LogOutput.Text += $"New message with i {i}\n";
    }
});

// Cleanup on close
window.Closed += (s, ee) =>
{
    StrongReferenceMessenger.Default.Unregister<CoreUserControl, string>(control, "Check Uncheck Design User Control");
};
```
`

JawadJaber avatar Jul 03 '25 13:07 JawadJaber