Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

WindowNotificationManager can't be used immediately after creating

Open 1746625968 opened this issue 3 years ago • 7 comments

Click the interface test button, the notification cannot be displayed and the error will be reported directly

@ForNeVeR @mterwoord @vmrocha @lontivero

 private void btn_TestClike(object sender, RoutedEventArgs e)
        {
            WindowNotificationManager _notificationManager = new WindowNotificationManager(this)
            {
                Position = NotificationPosition.TopRight,
                MaxItems = 3
            };

            _notificationManager.Show(new NotificationModel
            {
                Title = "1234",
                Message = "12345",
                Type = NotificationType.Information,
                Expiration = TimeSpan.FromSeconds(100000)
            });


            //NotificationManager.Show(new NotificationModel{ 
            //    Title = "Hey There!", 
            //    Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });

        }
public class NotificationModel: INotification
    {

        public string Title { get; set; }
            public string Message { get; set; }

            public NotificationType Type { get; set; }

            public TimeSpan Expiration { get; set; }

            //public Action OnClick { get; set; }

            //public Action OnClose { get; set; }



        public Action OnClick => () => Console.WriteLine("helloworld");

        public Action OnClose => () => Console.WriteLine("Stw");

    }

1746625968 avatar Feb 08 '21 05:02 1746625968

WindowNotificationManager can't be used immediately after object created. It needs to wait until its template will be applied. Also I don't see any reason to create new WindowNotificationManager per each button click. You can create single WindowNotificationManager per window in its constructor, for example. And on button click you can use already created and initialized notification manager.

maxkatz6 avatar Feb 08 '21 06:02 maxkatz6

However I think error message can be better in that case. FYI @danwalmsley

maxkatz6 avatar Feb 08 '21 06:02 maxkatz6

WindowNotificationManager _notificationManager = new WindowNotificationManager(this)
               {
                   Position = NotificationPosition.TopRight,
                   MaxItems = 3
               };

               _notificationManager.Show(new NotificationModel
               {
                   Title = "1234",
                   Message="12345"
               }) ;

I use it this way also report an error

1746625968 avatar Feb 08 '21 06:02 1746625968

As I said you can't use WindowNotificationManager right after it was created. You either need to split this code as I described above, or you can subscribe TemplateApplied in which handler you can show new notification.

maxkatz6 avatar Feb 08 '21 06:02 maxkatz6

https://github.com/zkSNACKs/WalletWasabi/blob/master/WalletWasabi.Gui/MainWindow.xaml.cs#L25-L39

you need to install it into a window like this...

you must not show a notification until after the window is shown.

call show inside a button click handler or command handler to ensure this.

danwalmsley avatar Feb 08 '21 14:02 danwalmsley

Probably too late but if anyone facing the same issue, this is the correct way to show it directly as per maxkatz6 instructions

var not = new Notification("Test", "this is a test notification message", NotificationType.Success);
        var nm = new WindowNotificationManager(this)
        {
            Position = NotificationPosition.BottomRight,
            MaxItems = 1
        };
        nm.TemplateApplied += (sender, args) =>
        {
            nm.Show(not);
        };

HossamElwahsh avatar Oct 14 '22 21:10 HossamElwahsh