WindowNotificationManager can't be used immediately after creating
1746625968 opened this issue ยท 8 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");
}
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.
However I think error message can be better in that case.
FYI @danwalmsley
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
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.
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.
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);
};
If somebody has the same problem I had. I was initializing it in the MainWindow's ctor but it did not show for me. Only after moving it to e.g. Window_OnLoaded:
private void Window_OnLoaded(object? sender, RoutedEventArgs e)
{
_notificationManager = new WindowNotificationManager(this)
{
Position = NotificationPosition.TopRight,
MaxItems = 5
};
}