Search code examples
c#.netavaloniauiavalonia

How to close a notification in Avalonia UI?


Having used Avalonia.Controls.Notifications i'm using Show(INotification) method to show notification. I can set a timer for notification to close it after timeout and can set an event handler for it to handle onClick. How can I close notification from my code by calling a method or any other way?

I tried to emulate click event but hadn't success


Solution

  • You have to get the instance of of NotificationCard that was created and call Close() method

    // NotificationManager is the instance of WindowNotificationManager used to show the notification
    var cards = NotificationManager.GetVisualChildren().FirstOrDefault() is ReversibleStackPanel panel ?
        panel.Children.OfType<NotificationCard>() : Array.Empty<NotificationCard>();
    
    // Close the first notification if exists
    cards.FirstOrDefault()?.Close();