diff --git a/samples/ControlCatalog/MainWindow.xaml.cs b/samples/ControlCatalog/MainWindow.xaml.cs index b5e315573d..91d9f034a5 100644 --- a/samples/ControlCatalog/MainWindow.xaml.cs +++ b/samples/ControlCatalog/MainWindow.xaml.cs @@ -21,7 +21,7 @@ namespace ControlCatalog //Renderer.DrawFps = true; //Renderer.DrawDirtyRects = Renderer.DrawFps = true; - _notificationArea = new WindowNotificationManager + _notificationArea = new WindowNotificationManager(this) { Position = NotificationPosition.TopRight, MaxItems = 3 @@ -39,12 +39,5 @@ namespace ControlCatalog theme.TryGetResource("Button", out _); AvaloniaXamlLoader.Load(this); } - - protected override void OnTemplateApplied(TemplateAppliedEventArgs e) - { - base.OnTemplateApplied(e); - - _notificationArea.Install(this); - } } } diff --git a/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs b/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs index 676ea230f2..d2a2b7b3bf 100644 --- a/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs +++ b/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Linq; +using System.Reactive.Linq; using System.Threading.Tasks; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; @@ -50,6 +51,26 @@ namespace Avalonia.Controls.Notifications set { SetValue(MaxItemsProperty, value); } } + public WindowNotificationManager(Window host) + { + if (VisualChildren.Count != 0) + { + Install(host); + } + else + { + IDisposable stopListening = null; + + stopListening = Observable.FromEventPattern(host, nameof(host.TemplateApplied)) + .Subscribe(_ => + { + stopListening.Dispose(); + + Install(host); + }); + } + } + static WindowNotificationManager() { PseudoClass(PositionProperty, x => x == NotificationPosition.TopLeft, ":topleft"); @@ -125,7 +146,7 @@ namespace Avalonia.Controls.Notifications _items.Remove(notification); } - public void Install(Window host) + private void Install(Window host) { var adornerLayer = host.GetVisualDescendants() .OfType()