From 768a7ce75d3587cd6ee1d94238bdc38dc3ac0669 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 21 Sep 2021 23:18:32 +0200 Subject: [PATCH 1/7] add info bar behavior --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 7 ++ .../StackedNotificationsBehavior.xaml | 19 ++++ .../StackedNotificationsBehavior.xaml.cs | 48 +++++++++ .../SamplePages/samples.json | 7 ++ .../InfoBar/InfoBarExtensions.cs | 17 +++ .../InfoBar/Notification.cs | 30 ++++++ .../InfoBar/StackedNotificationsBehavior.cs | 102 ++++++++++++++++++ .../Microsoft.Toolkit.Uwp.UI.Behaviors.csproj | 1 + 8 files changed, 231 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index 37ec112a0b2..f10651fc43e 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -519,6 +519,9 @@ RichSuggestBoxPage.xaml + + StackedNotificationsBehavior.xaml + TilesBrushPage.xaml @@ -1011,6 +1014,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + MSBuild:Compile Designer diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml new file mode 100644 index 00000000000..cc7c9deb810 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml @@ -0,0 +1,19 @@ + + + + + + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs new file mode 100644 index 00000000000..ea4253c6935 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs @@ -0,0 +1,48 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Toolkit.Uwp.UI.Behaviors; +using System; +using Windows.UI.Xaml.Controls; + +namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages +{ + public sealed partial class StackedNotificationsBehavior : Page + { + public StackedNotificationsBehavior() + { + InitializeComponent(); + Load(); + } + + private static string GetRandomText() + { + var random = new Random(); + var result = random.Next(1, 4); + + switch (result) + { + case 1: return "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sollicitudin bibendum enim at tincidunt. Praesent egestas ipsum ligula, nec tincidunt lacus semper non."; + case 2: return "Pellentesque in risus eget leo rhoncus ultricies nec id ante."; + case 3: default: return "Sed quis nisi quis nunc condimentum varius id consectetur metus. Duis mauris sapien, commodo eget erat ac, efficitur iaculis magna. Morbi eu velit nec massa pharetra cursus. Fusce non quam egestas leo finibus interdum eu ac massa. Quisque nec justo leo. Aenean scelerisque placerat ultrices. Sed accumsan lorem at arcu commodo tristique."; + } + } + + private void Load() + { + SampleController.Current.RegisterNewCommand( + "Show notification with random text", + (s, a) => + { + var notification = new Notification + { + Title = $"Notification {DateTimeOffset.Now}", + Message = GetRandomText(), + Duration = TimeSpan.FromSeconds(10), + }; + stackedNotificationBehavior.Show(notification); + }); + } + } +} diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index ecf8bf87cff..260b5982940 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -875,6 +875,13 @@ "Icon": "/Assets/Helpers.png", "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md" }, + { + "Name": "StackedNotificationsBehavior", + "Type": "StackedNotificationsBehavior", + "Subcategory": "Status and Info", + "About": "The behavior to add the stacked notification behavior to the WinUI InfoBar control.", + "Icon": "/SamplePages/InAppNotification/InAppNotification.png" + }, { "Name": "KeyDownTriggerBehavior", "Subcategory": "Systems", diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs new file mode 100644 index 00000000000..0c3d80a1463 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.UI.Xaml.Controls; + +namespace Microsoft.Toolkit.Uwp.UI.Behaviors +{ + internal static class InfoBarExtensions + { + public static void SetNotification(this InfoBar infoBar, Notification notification) + { + infoBar.Title = notification.Title; + infoBar.Message = notification.Message; + } + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs new file mode 100644 index 00000000000..583716683d6 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +namespace Microsoft.Toolkit.Uwp.UI.Behaviors +{ + /// + /// The content of a notification to display in + /// + public class Notification + { + /// + /// Gets or sets the notification title. + /// + public string Title { get; set; } + + /// + /// Gets or sets the notification message. + /// + public string Message { get; set; } + + /// + /// Gets or sets the duration of the notification. + /// Use for a persistent notification. + /// + public TimeSpan Duration { get; set; } + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs new file mode 100644 index 00000000000..9b9bb058c36 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs @@ -0,0 +1,102 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.UI.Xaml.Controls; +using System; +using System.Collections.Generic; +using Windows.System; + +namespace Microsoft.Toolkit.Uwp.UI.Behaviors +{ + /// + /// A behavior to add the stacked notification support to . + /// + public class StackedNotificationsBehavior : BehaviorBase + { + private readonly List _stackedNotifications; + private readonly DispatcherQueueTimer _dismissTimer; + + /// + /// Initializes a new instance of the class. + /// + public StackedNotificationsBehavior() + { + _stackedNotifications = new List(); + + var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + _dismissTimer = dispatcherQueue.CreateTimer(); + _dismissTimer.Tick += OnTimerTick; + } + + /// + /// Show . + /// + /// The notification to display. + public void Show(Notification notification) + { + _stackedNotifications.Add(notification); + ShowNext(); + } + + /// + protected override bool Initialize() + { + AssociatedObject.Closed += OnInfoBarClosed; + return true; + } + + /// + protected override bool Uninitialize() + { + AssociatedObject.Closed -= OnInfoBarClosed; + return true; + } + + private void OnInfoBarClosed(InfoBar sender, InfoBarClosedEventArgs args) + { + // We display the next notification. + ShowNext(); + } + + private void ShowNext() + { + if (AssociatedObject.IsOpen) + { + // One notification is already displayed. We wait for it to close + return; + } + + StopTimer(); + AssociatedObject.IsOpen = false; + + if (_stackedNotifications.Count == 0) + { + return; + } + + var notificationToDisplay = _stackedNotifications[0]; + _stackedNotifications.RemoveAt(0); + + AssociatedObject.SetNotification(notificationToDisplay); + AssociatedObject.IsOpen = true; + + StartTimer(notificationToDisplay.Duration); + } + + private void StartTimer(TimeSpan duration) + { + if (duration == default) + { + return; + } + + _dismissTimer.Interval = duration; + _dismissTimer.Start(); + } + + private void StopTimer() => _dismissTimer.Stop(); + + private void OnTimerTick(DispatcherQueueTimer sender, object args) => AssociatedObject.IsOpen = false; + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/Microsoft.Toolkit.Uwp.UI.Behaviors.csproj b/Microsoft.Toolkit.Uwp.UI.Behaviors/Microsoft.Toolkit.Uwp.UI.Behaviors.csproj index 81dfa301b10..a0910df9da1 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/Microsoft.Toolkit.Uwp.UI.Behaviors.csproj +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/Microsoft.Toolkit.Uwp.UI.Behaviors.csproj @@ -23,6 +23,7 @@ + From 468d4ea9fad72a3cbf56f0ecd09642d324ddcfd2 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 18 Nov 2021 22:47:35 +0100 Subject: [PATCH 2/7] add mouse over detection --- .../InfoBar/Notification.cs | 4 +-- .../InfoBar/StackedNotificationsBehavior.cs | 28 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs index 583716683d6..12153e8fa40 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs @@ -23,8 +23,8 @@ public class Notification /// /// Gets or sets the duration of the notification. - /// Use for a persistent notification. + /// Set to null for persistent notification. /// - public TimeSpan Duration { get; set; } + public TimeSpan? Duration { get; set; } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs index 9b9bb058c36..7dcc41a70ae 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs @@ -14,15 +14,16 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors /// public class StackedNotificationsBehavior : BehaviorBase { - private readonly List _stackedNotifications; + private readonly Queue _stackedNotifications; private readonly DispatcherQueueTimer _dismissTimer; + private Notification _currentNotification; /// /// Initializes a new instance of the class. /// public StackedNotificationsBehavior() { - _stackedNotifications = new List(); + _stackedNotifications = new Queue(); var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); _dismissTimer = dispatcherQueue.CreateTimer(); @@ -35,7 +36,7 @@ public StackedNotificationsBehavior() /// The notification to display. public void Show(Notification notification) { - _stackedNotifications.Add(notification); + _stackedNotifications.Enqueue(notification); ShowNext(); } @@ -43,6 +44,8 @@ public void Show(Notification notification) protected override bool Initialize() { AssociatedObject.Closed += OnInfoBarClosed; + AssociatedObject.PointerEntered += OnPointerEntered; + AssociatedObject.PointerExited += OnPointedExited; return true; } @@ -50,6 +53,8 @@ protected override bool Initialize() protected override bool Uninitialize() { AssociatedObject.Closed -= OnInfoBarClosed; + AssociatedObject.PointerEntered -= OnPointerEntered; + AssociatedObject.PointerExited -= OnPointedExited; return true; } @@ -70,33 +75,36 @@ private void ShowNext() StopTimer(); AssociatedObject.IsOpen = false; - if (_stackedNotifications.Count == 0) + if (!_stackedNotifications.TryDequeue(out var notificationToDisplay)) { + _currentNotification = null; return; } - var notificationToDisplay = _stackedNotifications[0]; - _stackedNotifications.RemoveAt(0); - + _currentNotification = notificationToDisplay; AssociatedObject.SetNotification(notificationToDisplay); AssociatedObject.IsOpen = true; StartTimer(notificationToDisplay.Duration); } - private void StartTimer(TimeSpan duration) + private void StartTimer(TimeSpan? duration) { - if (duration == default) + if (duration is null) { return; } - _dismissTimer.Interval = duration; + _dismissTimer.Interval = duration.Value; _dismissTimer.Start(); } private void StopTimer() => _dismissTimer.Stop(); private void OnTimerTick(DispatcherQueueTimer sender, object args) => AssociatedObject.IsOpen = false; + + private void OnPointedExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) => StartTimer(_currentNotification?.Duration); + + private void OnPointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) => StopTimer(); } } From 00ae94b72fe054662e40e57e9e75b96ab1100c7f Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 18 Nov 2021 23:12:47 +0100 Subject: [PATCH 3/7] add additional notification parameters --- .../StackedNotificationsBehavior.xaml.cs | 44 ++++++++++++++++++- .../InfoBar/InfoBarExtensions.cs | 10 +++++ .../InfoBar/Notification.cs | 31 +++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs index ea4253c6935..e9d785386b9 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs @@ -32,7 +32,7 @@ private static string GetRandomText() private void Load() { SampleController.Current.RegisterNewCommand( - "Show notification with random text", + "Show information notification", (s, a) => { var notification = new Notification @@ -40,6 +40,48 @@ private void Load() Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), Duration = TimeSpan.FromSeconds(10), + Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational, + }; + stackedNotificationBehavior.Show(notification); + }); + SampleController.Current.RegisterNewCommand( + "Show error notification", + (s, a) => + { + var notification = new Notification + { + Title = $"Notification {DateTimeOffset.Now}", + Message = GetRandomText(), + Duration = TimeSpan.FromSeconds(10), + Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Error, + }; + stackedNotificationBehavior.Show(notification); + }); + SampleController.Current.RegisterNewCommand( + "Show notification with action", + (s, a) => + { + var notification = new Notification + { + Title = $"Notification {DateTimeOffset.Now}", + Message = GetRandomText(), + Duration = TimeSpan.FromSeconds(10), + Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, + ActionButton = new Button { Content = "Action" } + }; + stackedNotificationBehavior.Show(notification); + }); + SampleController.Current.RegisterNewCommand( + "Show notification with custom content", + (s, a) => + { + var notification = new Notification + { + Title = $"Notification {DateTimeOffset.Now}", + Message = GetRandomText(), + Duration = TimeSpan.FromSeconds(10), + Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, + Content = new TextBlock { Text = "Custom content" } }; stackedNotificationBehavior.Show(notification); }); diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs index 0c3d80a1463..7c72d3319b4 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs @@ -8,10 +8,20 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors { internal static class InfoBarExtensions { + /// + /// Sets the content on the . + /// + /// The to update. + /// The notification to set on the . public static void SetNotification(this InfoBar infoBar, Notification notification) { infoBar.Title = notification.Title; infoBar.Message = notification.Message; + infoBar.Severity = notification.Severity; + infoBar.IsIconVisible = notification.IsIconVisible; + infoBar.Content = notification.Content; + infoBar.ContentTemplate = notification.ContentTemplate; + infoBar.ActionButton = notification.ActionButton; } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs index 12153e8fa40..daecf51d673 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs @@ -2,7 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Microsoft.UI.Xaml.Controls; using System; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls.Primitives; namespace Microsoft.Toolkit.Uwp.UI.Behaviors { @@ -21,6 +24,34 @@ public class Notification /// public string Message { get; set; } + /// + /// Gets or sets the type of the to apply consistent status color, icon, + /// and assistive technology settings dependent on the criticality of the notification. + /// + public InfoBarSeverity Severity { get; set; } + + /// + /// Gets or sets a value that indicates whether the icon is visible in the InfoBar. + /// true if the icon is visible; otherwise, false. The default is true. + /// + public bool IsIconVisible { get; set; } = true; + + /// + /// Gets or sets the XAML Content that is displayed below the title and message in + /// the InfoBar. + /// + public object Content { get; set; } + + /// + /// Gets or sets the data template for the . + /// + public DataTemplate ContentTemplate { get; set; } + + /// + /// Gets or sets the action button of the InfoBar. + /// + public ButtonBase ActionButton { get; set; } + /// /// Gets or sets the duration of the notification. /// Set to null for persistent notification. From 68dcfb10aa2685274ac65a17674f022066948830 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 27 Nov 2021 23:29:25 +0100 Subject: [PATCH 4/7] finalized implementation and add NotificationWithOverrides for advanced scenarios --- .../StackedNotificationsBehavior.xaml | 9 ++ .../StackedNotificationsBehavior.xaml.cs | 22 +++-- .../InfoBar/InfoBarExtensions.cs | 27 ------ .../InfoBar/Notification.cs | 39 ++------ .../InfoBar/NotificationWithOverrides.cs | 90 +++++++++++++++++++ .../InfoBar/StackedNotificationsBehavior.cs | 71 ++++++++++++++- 6 files changed, 193 insertions(+), 65 deletions(-) delete mode 100644 Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml index cc7c9deb810..4dc289d6168 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml @@ -15,5 +15,14 @@ + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs index e9d785386b9..d9334815b57 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs @@ -39,7 +39,7 @@ private void Load() { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), - Duration = TimeSpan.FromSeconds(10), + Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational, }; stackedNotificationBehavior.Show(notification); @@ -52,7 +52,7 @@ private void Load() { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), - Duration = TimeSpan.FromSeconds(10), + Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Error, }; stackedNotificationBehavior.Show(notification); @@ -61,11 +61,11 @@ private void Load() "Show notification with action", (s, a) => { - var notification = new Notification + var notification = new NotificationWithOverrides { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), - Duration = TimeSpan.FromSeconds(10), + Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, ActionButton = new Button { Content = "Action" } }; @@ -75,16 +75,26 @@ private void Load() "Show notification with custom content", (s, a) => { - var notification = new Notification + var notification = new NotificationWithOverrides { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), - Duration = TimeSpan.FromSeconds(10), + Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, Content = new TextBlock { Text = "Custom content" } }; stackedNotificationBehavior.Show(notification); }); } + + private TimeSpan? GetDuration() + { + if(!int.TryParse(NotificationDurationTextBox.Text, out var duration)) + { + return null; + } + + return TimeSpan.FromMilliseconds(duration); + } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs deleted file mode 100644 index 7c72d3319b4..00000000000 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/InfoBarExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Microsoft.UI.Xaml.Controls; - -namespace Microsoft.Toolkit.Uwp.UI.Behaviors -{ - internal static class InfoBarExtensions - { - /// - /// Sets the content on the . - /// - /// The to update. - /// The notification to set on the . - public static void SetNotification(this InfoBar infoBar, Notification notification) - { - infoBar.Title = notification.Title; - infoBar.Message = notification.Message; - infoBar.Severity = notification.Severity; - infoBar.IsIconVisible = notification.IsIconVisible; - infoBar.Content = notification.Content; - infoBar.ContentTemplate = notification.ContentTemplate; - infoBar.ActionButton = notification.ActionButton; - } - } -} diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs index daecf51d673..7f9a81850b4 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs @@ -4,13 +4,12 @@ using Microsoft.UI.Xaml.Controls; using System; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls.Primitives; namespace Microsoft.Toolkit.Uwp.UI.Behaviors { /// - /// The content of a notification to display in + /// The content of a notification to display in . + /// All the values will be applied to the targeted . /// public class Notification { @@ -24,38 +23,16 @@ public class Notification /// public string Message { get; set; } - /// - /// Gets or sets the type of the to apply consistent status color, icon, - /// and assistive technology settings dependent on the criticality of the notification. - /// - public InfoBarSeverity Severity { get; set; } - - /// - /// Gets or sets a value that indicates whether the icon is visible in the InfoBar. - /// true if the icon is visible; otherwise, false. The default is true. - /// - public bool IsIconVisible { get; set; } = true; - - /// - /// Gets or sets the XAML Content that is displayed below the title and message in - /// the InfoBar. - /// - public object Content { get; set; } - - /// - /// Gets or sets the data template for the . - /// - public DataTemplate ContentTemplate { get; set; } - - /// - /// Gets or sets the action button of the InfoBar. - /// - public ButtonBase ActionButton { get; set; } - /// /// Gets or sets the duration of the notification. /// Set to null for persistent notification. /// public TimeSpan? Duration { get; set; } + + /// + /// Gets or sets the type of the to apply consistent status color, icon, + /// and assistive technology settings dependent on the criticality of the notification. + /// + public InfoBarSeverity Severity { get; set; } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs new file mode 100644 index 00000000000..2d0f2757856 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs @@ -0,0 +1,90 @@ +using System; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls.Primitives; + +namespace Microsoft.Toolkit.Uwp.UI.Behaviors +{ + /// + /// An extended notification request. + /// It allows the user to override different parts of the targeted . + /// Only the explicitly set properties will be applied to the InfoBar. + /// + public class NotificationWithOverrides : Notification + { + private NotificationOverrides _overrides; + private bool _isIconVisible; + private object _content; + private DataTemplate _contentTemplate; + private ButtonBase _actionButton; + + /// + /// Gets or sets a value indicating whether the icon is visible or not. + /// True if the icon is visible; otherwise, false. The default is true. + /// + public bool IsIconVisible + { + get => _isIconVisible; + set + { + _isIconVisible = value; + _overrides |= NotificationOverrides.Icon; + } + } + + /// + /// Gets or sets the XAML Content that is displayed below the title and message in + /// the InfoBar. + /// + public object Content + { + get => _content; + set + { + _content = value; + _overrides |= NotificationOverrides.Content; + } + } + + /// + /// Gets or sets the data template for the . + /// + public DataTemplate ContentTemplate + { + get => _contentTemplate; + set + { + _contentTemplate = value; + _overrides |= NotificationOverrides.ContentTemplate; + } + } + + /// + /// Gets or sets the action button of the InfoBar. + /// + public ButtonBase ActionButton + { + get => _actionButton; + set + { + _actionButton = value; + _overrides |= NotificationOverrides.ActionButton; + } + } + + internal NotificationOverrides Overrides => _overrides; + } + + /// + /// The overrides which should be set on the notification. + /// + [Flags] + internal enum NotificationOverrides + { + None, + Icon, + Content, + ContentTemplate, + ActionButton, + + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs index 7dcc41a70ae..4fb2031a9b8 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs @@ -6,6 +6,8 @@ using System; using System.Collections.Generic; using Windows.System; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls.Primitives; namespace Microsoft.Toolkit.Uwp.UI.Behaviors { @@ -17,6 +19,10 @@ public class StackedNotificationsBehavior : BehaviorBase private readonly Queue _stackedNotifications; private readonly DispatcherQueueTimer _dismissTimer; private Notification _currentNotification; + private bool _initialIconVisible; + private object _initialContent; + private DataTemplate _initialContentTemplate; + private ButtonBase _initialActionButton; /// /// Initializes a new instance of the class. @@ -74,6 +80,7 @@ private void ShowNext() StopTimer(); AssociatedObject.IsOpen = false; + RestoreOverridenProperties(); if (!_stackedNotifications.TryDequeue(out var notificationToDisplay)) { @@ -82,12 +89,74 @@ private void ShowNext() } _currentNotification = notificationToDisplay; - AssociatedObject.SetNotification(notificationToDisplay); + SetNotification(notificationToDisplay); AssociatedObject.IsOpen = true; StartTimer(notificationToDisplay.Duration); } + private void SetNotification(Notification notification) + { + AssociatedObject.Title = notification.Title; + AssociatedObject.Message = notification.Message; + AssociatedObject.Severity = notification.Severity; + + if (notification is NotificationWithOverrides overrides) + { + if (overrides.Overrides.HasFlag(NotificationOverrides.Icon)) + { + _initialIconVisible = AssociatedObject.IsIconVisible; + AssociatedObject.IsIconVisible = overrides.IsIconVisible; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.Content)) + { + _initialContent = AssociatedObject.Content; + AssociatedObject.Content = overrides.Content; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.ContentTemplate)) + { + _initialContentTemplate = AssociatedObject.ContentTemplate; + AssociatedObject.ContentTemplate = overrides.ContentTemplate; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.ActionButton)) + { + _initialActionButton = AssociatedObject.ActionButton; + AssociatedObject.ActionButton = overrides.ActionButton; + } + } + } + + private void RestoreOverridenProperties() + { + if (_currentNotification is not NotificationWithOverrides overrides) + { + return; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.Icon)) + { + AssociatedObject.IsIconVisible = _initialIconVisible; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.Content)) + { + AssociatedObject.Content = _initialContent; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.ContentTemplate)) + { + AssociatedObject.ContentTemplate = _initialContentTemplate; + } + + if (overrides.Overrides.HasFlag(NotificationOverrides.ActionButton)) + { + AssociatedObject.ActionButton = _initialActionButton; + } + } + private void StartTimer(TimeSpan? duration) { if (duration is null) From 0469293545c48c47948d27b48f24bc0496c6bb9b Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 27 Nov 2021 23:47:50 +0100 Subject: [PATCH 5/7] fix bug --- .../StackedNotificationsBehavior.xaml | 8 +++++--- .../StackedNotificationsBehavior.xaml.cs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml index 4dc289d6168..66f12399dab 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml @@ -10,14 +10,16 @@ mc:Ignorable="d"> - + - + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs index d9334815b57..18c7e652352 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs @@ -89,7 +89,7 @@ private void Load() private TimeSpan? GetDuration() { - if(!int.TryParse(NotificationDurationTextBox.Text, out var duration)) + if(!int.TryParse(NotificationDurationTextBox.Text, out var duration) || duration <= 0) { return null; } From cd0ed6c05f6deeef9d9dd7f9a9ec5cc1d431c752 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 2 Dec 2021 23:22:34 +0100 Subject: [PATCH 6/7] remove NotificationsWithOverride + add remove support --- .../StackedNotificationsBehavior.xaml.cs | 29 ++++-- .../InfoBar/Notification.cs | 82 +++++++++++++++- .../InfoBar/NotificationWithOverrides.cs | 90 ------------------ .../InfoBar/StackedNotificationsBehavior.cs | 95 ++++++++++++------- 4 files changed, 164 insertions(+), 132 deletions(-) delete mode 100644 Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs index 18c7e652352..b891abbb307 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs @@ -10,6 +10,8 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages { public sealed partial class StackedNotificationsBehavior : Page { + private Notification _lastNotification; + public StackedNotificationsBehavior() { InitializeComponent(); @@ -35,33 +37,33 @@ private void Load() "Show information notification", (s, a) => { - var notification = new Notification + _lastNotification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational, }; - stackedNotificationBehavior.Show(notification); + stackedNotificationBehavior.Show(_lastNotification); }); SampleController.Current.RegisterNewCommand( "Show error notification", (s, a) => { - var notification = new Notification + _lastNotification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Error, }; - stackedNotificationBehavior.Show(notification); + stackedNotificationBehavior.Show(_lastNotification); }); SampleController.Current.RegisterNewCommand( "Show notification with action", (s, a) => { - var notification = new NotificationWithOverrides + _lastNotification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), @@ -69,13 +71,13 @@ private void Load() Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, ActionButton = new Button { Content = "Action" } }; - stackedNotificationBehavior.Show(notification); + stackedNotificationBehavior.Show(_lastNotification); }); SampleController.Current.RegisterNewCommand( "Show notification with custom content", (s, a) => { - var notification = new NotificationWithOverrides + _lastNotification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), @@ -83,7 +85,18 @@ private void Load() Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, Content = new TextBlock { Text = "Custom content" } }; - stackedNotificationBehavior.Show(notification); + stackedNotificationBehavior.Show(_lastNotification); + }); + SampleController.Current.RegisterNewCommand( + "Remove last notification", + (s, a) => + { + if (_lastNotification is null) + { + return; + } + + stackedNotificationBehavior.Remove(_lastNotification); }); } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs index 7f9a81850b4..50fc5aaf862 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/Notification.cs @@ -4,15 +4,26 @@ using Microsoft.UI.Xaml.Controls; using System; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml; namespace Microsoft.Toolkit.Uwp.UI.Behaviors { /// /// The content of a notification to display in . - /// All the values will be applied to the targeted . + /// The , , and values will + /// always be applied to the targeted . + /// The , , and values + /// will be applied only if set. /// public class Notification { + private NotificationOverrides _overrides; + private bool _isIconVisible; + private object _content; + private DataTemplate _contentTemplate; + private ButtonBase _actionButton; + /// /// Gets or sets the notification title. /// @@ -34,5 +45,74 @@ public class Notification /// and assistive technology settings dependent on the criticality of the notification. /// public InfoBarSeverity Severity { get; set; } + + /// + /// Gets or sets a value indicating whether the icon is visible or not. + /// True if the icon is visible; otherwise, false. The default is true. + /// + public bool IsIconVisible + { + get => _isIconVisible; + set + { + _isIconVisible = value; + _overrides |= NotificationOverrides.Icon; + } + } + + /// + /// Gets or sets the XAML Content that is displayed below the title and message in + /// the InfoBar. + /// + public object Content + { + get => _content; + set + { + _content = value; + _overrides |= NotificationOverrides.Content; + } + } + + /// + /// Gets or sets the data template for the . + /// + public DataTemplate ContentTemplate + { + get => _contentTemplate; + set + { + _contentTemplate = value; + _overrides |= NotificationOverrides.ContentTemplate; + } + } + + /// + /// Gets or sets the action button of the InfoBar. + /// + public ButtonBase ActionButton + { + get => _actionButton; + set + { + _actionButton = value; + _overrides |= NotificationOverrides.ActionButton; + } + } + + internal NotificationOverrides Overrides => _overrides; + } + + /// + /// The overrides which should be set on the notification. + /// + [Flags] + internal enum NotificationOverrides + { + None, + Icon, + Content, + ContentTemplate, + ActionButton, } } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs deleted file mode 100644 index 2d0f2757856..00000000000 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/NotificationWithOverrides.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls.Primitives; - -namespace Microsoft.Toolkit.Uwp.UI.Behaviors -{ - /// - /// An extended notification request. - /// It allows the user to override different parts of the targeted . - /// Only the explicitly set properties will be applied to the InfoBar. - /// - public class NotificationWithOverrides : Notification - { - private NotificationOverrides _overrides; - private bool _isIconVisible; - private object _content; - private DataTemplate _contentTemplate; - private ButtonBase _actionButton; - - /// - /// Gets or sets a value indicating whether the icon is visible or not. - /// True if the icon is visible; otherwise, false. The default is true. - /// - public bool IsIconVisible - { - get => _isIconVisible; - set - { - _isIconVisible = value; - _overrides |= NotificationOverrides.Icon; - } - } - - /// - /// Gets or sets the XAML Content that is displayed below the title and message in - /// the InfoBar. - /// - public object Content - { - get => _content; - set - { - _content = value; - _overrides |= NotificationOverrides.Content; - } - } - - /// - /// Gets or sets the data template for the . - /// - public DataTemplate ContentTemplate - { - get => _contentTemplate; - set - { - _contentTemplate = value; - _overrides |= NotificationOverrides.ContentTemplate; - } - } - - /// - /// Gets or sets the action button of the InfoBar. - /// - public ButtonBase ActionButton - { - get => _actionButton; - set - { - _actionButton = value; - _overrides |= NotificationOverrides.ActionButton; - } - } - - internal NotificationOverrides Overrides => _overrides; - } - - /// - /// The overrides which should be set on the notification. - /// - [Flags] - internal enum NotificationOverrides - { - None, - Icon, - Content, - ContentTemplate, - ActionButton, - - } -} diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs index 4fb2031a9b8..011154d2015 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/InfoBar/StackedNotificationsBehavior.cs @@ -16,7 +16,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors /// public class StackedNotificationsBehavior : BehaviorBase { - private readonly Queue _stackedNotifications; + private readonly LinkedList _stackedNotifications; private readonly DispatcherQueueTimer _dismissTimer; private Notification _currentNotification; private bool _initialIconVisible; @@ -29,7 +29,7 @@ public class StackedNotificationsBehavior : BehaviorBase /// public StackedNotificationsBehavior() { - _stackedNotifications = new Queue(); + _stackedNotifications = new LinkedList(); var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); _dismissTimer = dispatcherQueue.CreateTimer(); @@ -42,10 +42,39 @@ public StackedNotificationsBehavior() /// The notification to display. public void Show(Notification notification) { - _stackedNotifications.Enqueue(notification); + if (notification is null) + { + throw new ArgumentNullException(nameof(notification)); + } + + _stackedNotifications.AddLast(notification); ShowNext(); } + /// + /// Remove the . + /// If the notification is displayed, it will be closed. + /// If the notification is still in the queue, it will be removed. + /// + /// The notification to remove. + public void Remove(Notification notification) + { + if (notification is null) + { + throw new ArgumentNullException(nameof(notification)); + } + + if (notification == _currentNotification) + { + // We close the notification. This will trigger the display of the next one. + // See OnInfoBarClosed. + AssociatedObject.IsOpen = false; + return; + } + + _stackedNotifications.Remove(notification); + } + /// protected override bool Initialize() { @@ -82,12 +111,15 @@ private void ShowNext() AssociatedObject.IsOpen = false; RestoreOverridenProperties(); - if (!_stackedNotifications.TryDequeue(out var notificationToDisplay)) + if (_stackedNotifications.Count == 0) { _currentNotification = null; return; } + var notificationToDisplay = _stackedNotifications.First.Value; + _stackedNotifications.RemoveFirst(); + _currentNotification = notificationToDisplay; SetNotification(notificationToDisplay); AssociatedObject.IsOpen = true; @@ -101,57 +133,54 @@ private void SetNotification(Notification notification) AssociatedObject.Message = notification.Message; AssociatedObject.Severity = notification.Severity; - if (notification is NotificationWithOverrides overrides) + if (notification.Overrides.HasFlag(NotificationOverrides.Icon)) + { + _initialIconVisible = AssociatedObject.IsIconVisible; + AssociatedObject.IsIconVisible = notification.IsIconVisible; + } + + if (notification.Overrides.HasFlag(NotificationOverrides.Content)) + { + _initialContent = AssociatedObject.Content; + AssociatedObject.Content = notification.Content; + } + + if (notification.Overrides.HasFlag(NotificationOverrides.ContentTemplate)) + { + _initialContentTemplate = AssociatedObject.ContentTemplate; + AssociatedObject.ContentTemplate = notification.ContentTemplate; + } + + if (notification.Overrides.HasFlag(NotificationOverrides.ActionButton)) { - if (overrides.Overrides.HasFlag(NotificationOverrides.Icon)) - { - _initialIconVisible = AssociatedObject.IsIconVisible; - AssociatedObject.IsIconVisible = overrides.IsIconVisible; - } - - if (overrides.Overrides.HasFlag(NotificationOverrides.Content)) - { - _initialContent = AssociatedObject.Content; - AssociatedObject.Content = overrides.Content; - } - - if (overrides.Overrides.HasFlag(NotificationOverrides.ContentTemplate)) - { - _initialContentTemplate = AssociatedObject.ContentTemplate; - AssociatedObject.ContentTemplate = overrides.ContentTemplate; - } - - if (overrides.Overrides.HasFlag(NotificationOverrides.ActionButton)) - { - _initialActionButton = AssociatedObject.ActionButton; - AssociatedObject.ActionButton = overrides.ActionButton; - } + _initialActionButton = AssociatedObject.ActionButton; + AssociatedObject.ActionButton = notification.ActionButton; } } private void RestoreOverridenProperties() { - if (_currentNotification is not NotificationWithOverrides overrides) + if (_currentNotification is null) { return; } - if (overrides.Overrides.HasFlag(NotificationOverrides.Icon)) + if (_currentNotification.Overrides.HasFlag(NotificationOverrides.Icon)) { AssociatedObject.IsIconVisible = _initialIconVisible; } - if (overrides.Overrides.HasFlag(NotificationOverrides.Content)) + if (_currentNotification.Overrides.HasFlag(NotificationOverrides.Content)) { AssociatedObject.Content = _initialContent; } - if (overrides.Overrides.HasFlag(NotificationOverrides.ContentTemplate)) + if (_currentNotification.Overrides.HasFlag(NotificationOverrides.ContentTemplate)) { AssociatedObject.ContentTemplate = _initialContentTemplate; } - if (overrides.Overrides.HasFlag(NotificationOverrides.ActionButton)) + if (_currentNotification.Overrides.HasFlag(NotificationOverrides.ActionButton)) { AssociatedObject.ActionButton = _initialActionButton; } From e162811777a49e7c5e108f73d0f892902d906592 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 Jan 2022 22:50:46 +0100 Subject: [PATCH 7/7] remove dismiss button from sample --- .../StackedNotificationsBehavior.xaml.cs | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs index b891abbb307..cc24d513758 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/StackedNotificationsBehavior/StackedNotificationsBehavior.xaml.cs @@ -10,7 +10,7 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages { public sealed partial class StackedNotificationsBehavior : Page { - private Notification _lastNotification; + private Notification notification; public StackedNotificationsBehavior() { @@ -37,33 +37,33 @@ private void Load() "Show information notification", (s, a) => { - _lastNotification = new Notification + var notification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational, }; - stackedNotificationBehavior.Show(_lastNotification); + stackedNotificationBehavior.Show(notification); }); SampleController.Current.RegisterNewCommand( "Show error notification", (s, a) => { - _lastNotification = new Notification + var notification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), Duration = GetDuration(), Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Error, }; - stackedNotificationBehavior.Show(_lastNotification); + stackedNotificationBehavior.Show(notification); }); SampleController.Current.RegisterNewCommand( "Show notification with action", (s, a) => { - _lastNotification = new Notification + var notification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), @@ -71,13 +71,13 @@ private void Load() Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, ActionButton = new Button { Content = "Action" } }; - stackedNotificationBehavior.Show(_lastNotification); + stackedNotificationBehavior.Show(notification); }); SampleController.Current.RegisterNewCommand( "Show notification with custom content", (s, a) => { - _lastNotification = new Notification + var notification = new Notification { Title = $"Notification {DateTimeOffset.Now}", Message = GetRandomText(), @@ -85,18 +85,7 @@ private void Load() Severity = Microsoft.UI.Xaml.Controls.InfoBarSeverity.Warning, Content = new TextBlock { Text = "Custom content" } }; - stackedNotificationBehavior.Show(_lastNotification); - }); - SampleController.Current.RegisterNewCommand( - "Remove last notification", - (s, a) => - { - if (_lastNotification is null) - { - return; - } - - stackedNotificationBehavior.Remove(_lastNotification); + stackedNotificationBehavior.Show(notification); }); }