Windows icon indicating copy to clipboard operation
Windows copied to clipboard

NullReferenceException in CommunityToolkit.WinUI.Behaviors StackedNotificationsBehavior.ShowNext

Open mfeingol opened this issue 1 year ago • 3 comments

Describe the bug

I have not reproduced this locally, but I'm seeing this NullReferenceException occasionally from user crash reports:

CommunityToolkit.WinUI.Behaviors
StackedNotificationsBehavior.ShowNext ()
CommunityToolkit.WinUI.Behaviors
StackedNotificationsBehavior.Show (Notification)
CommunityToolkit.WinUI.Behaviors
StackedNotificationsBehavior.Show (String, Int32, String)

Steps to reproduce

Unknown, unfortunately. My guess is the StackedNotificationsBehavior was used after its hosting ContentDialog had been closed.

Expected behavior

I'd expect this component to not throw a NullReferenceException, even if used after being uninitialized.

Screenshots

No response

Code Platform

  • [X] UWP
  • [ ] WinAppSDK / WinUI 3
  • [ ] Web Assembly (WASM)
  • [ ] Android
  • [ ] iOS
  • [ ] MacOS
  • [ ] Linux / GTK

Windows Build Number

  • [ ] Windows 10 1809 (Build 17763)
  • [ ] Windows 10 1903 (Build 18362)
  • [ ] Windows 10 1909 (Build 18363)
  • [ ] Windows 10 2004 (Build 19041)
  • [ ] Windows 10 20H2 (Build 19042)
  • [ ] Windows 10 21H1 (Build 19043)
  • [ ] Windows 10 21H2 (Build 19044)
  • [X] Windows 10 22H2 (Build 19045)
  • [ ] Windows 11 21H2 (Build 22000)
  • [ ] Other (specify)

Other Windows Build number

No response

App minimum and target SDK version

  • [ ] Windows 10, version 1809 (Build 17763)
  • [ ] Windows 10, version 1903 (Build 18362)
  • [ ] Windows 10, version 1909 (Build 18363)
  • [X] Windows 10, version 2004 (Build 19041)
  • [ ] Windows 10, version 2104 (Build 20348)
  • [ ] Windows 11, version 22H2 (Build 22000)
  • [X] Other (specify)

Other SDK version

22621

Visual Studio Version

2022

Visual Studio Build Number

17.10.1

Device form factor

Desktop

Additional context

No response

Help us help you

Yes, but only if others can assist.

mfeingol avatar Jun 07 '24 00:06 mfeingol

Landed a local repro. The problem is that the AssociatedObject property is null. Presumably the StackedNotificationsBehavior should either return silently or throw InvalidOperationException or some such.

mfeingol avatar Jun 07 '24 01:06 mfeingol

Just ran into this. I am trying to use a StackedNotificationsBehavior inside a ContentDialog. The ContentDialog is not closing at the time (at least, it shouldn't be becuase close is cancelled).

Any idea why AssociatedObject would be null in our cases?

beeradmoore avatar Jan 17 '25 23:01 beeradmoore

I ended up doing this simple workaround. Not sure if it's hidden legit notifications or not:

public void Show(string message, TimeSpan duration)
{
    if (this.StackedNotificationsBehavior.AssociatedObject != null)
        this.StackedNotificationsBehavior.Show(message, (int)Math.Round(duration.TotalMilliseconds));
}

mfeingol avatar Jan 22 '25 18:01 mfeingol