diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationCode.bind
index 5b656636a0b..b151f91a4fe 100644
--- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationCode.bind
+++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationCode.bind
@@ -46,6 +46,14 @@ if (isTemplatePresent && inAppNotificationWithButtonsTemplate is DataTemplate)
ExampleInAppNotification.Show(inAppNotificationWithButtonsTemplate as DataTemplate);
}
+// Show notification using an object
+
+
+var notificationData = new MyNotificationData("Title", "Message");
+ExampleInAppNotification.Show(notificationData, duration: 2000);
+
// Dismiss notification
ExampleInAppNotification.Dismiss();
@@ -53,14 +61,14 @@ ExampleInAppNotification.Dismiss();
private void YesButton_Click(object sender, RoutedEventArgs e)
{
- // TODO : Do something when user accepted
+ // TODO : Do something when user accepted
ExampleInAppNotification.Dismiss();
}
private void NoButton_Click(object sender, RoutedEventArgs e)
{
- // TODO : Do something when user refused
+ // TODO : Do something when user refused
ExampleInAppNotification.Dismiss();
}
\ No newline at end of file
diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationPage.xaml.cs
index 6a5f814e15a..cb35082fd6f 100644
--- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationPage.xaml.cs
+++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationPage.xaml.cs
@@ -2,10 +2,11 @@
// 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;
-using System.Windows.Input;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Extensions;
+using System;
+using System.Collections.Generic;
+using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@@ -57,6 +58,15 @@ private void Load()
_exampleInAppNotification?.Show(GetRandomText(), NotificationDuration);
});
+ SampleController.Current.RegisterNewCommand("Show notification with object", (sender, args) =>
+ {
+ _exampleVSCodeInAppNotification?.Dismiss();
+ SetDefaultControlTemplate();
+
+ var random = new Random();
+ _exampleInAppNotification?.Show(new KeyValuePair(random.Next(1, 10), GetRandomText()), NotificationDuration);
+ });
+
SampleController.Current.RegisterNewCommand("Show notification with buttons (without DataTemplate)", (sender, args) =>
{
_exampleVSCodeInAppNotification?.Dismiss();
diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationXaml.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationXaml.bind
index 0b5afac336d..812d93e65cd 100644
--- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationXaml.bind
+++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationXaml.bind
@@ -153,7 +153,8 @@
-
-
+ StackMode="@[StackMode:Enum:StackMode.Replace]">
+
+
+
+
+
+
+
+
+
+
private const string DismissButtonPart = "PART_DismissButton";
+
+ ///
+ /// Key of the UI Element that will display the notification content.
+ ///
+ private const string ContentPresenterPart = "PART_Presenter";
}
}
diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.Events.cs
index 107f55a0342..32282d70de9 100644
--- a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.Events.cs
+++ b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.Events.cs
@@ -35,6 +35,19 @@ public partial class InAppNotification
///
public event InAppNotificationClosedEventHandler Closed;
+ private static void AutomateTextNotification(AutomationPeer peer, string message)
+ {
+ if (peer != null)
+ {
+ peer.SetFocus();
+ peer.RaiseNotificationEvent(
+ AutomationNotificationKind.Other,
+ AutomationNotificationProcessing.ImportantMostRecent,
+ StringExtensions.GetLocalized("WindowsCommunityToolkit_InAppNotification_Events_NewNotificationMessage", "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/Microsoft.Toolkit.Uwp.UI.Controls/Resources") + message,
+ Guid.NewGuid().ToString());
+ }
+ }
+
private void DismissButton_Click(object sender, RoutedEventArgs e)
{
Dismiss(InAppNotificationDismissKind.User);
@@ -45,44 +58,37 @@ private void DismissTimer_Tick(object sender, object e)
Dismiss(InAppNotificationDismissKind.Timeout);
}
- private void OpenAnimationTimer_Tick(object sender, object e)
+ private void OnCurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
- lock (_openAnimationTimer)
+ switch (e.NewState.Name)
{
- _openAnimationTimer.Stop();
- Opened?.Invoke(this, EventArgs.Empty);
- SetValue(AutomationProperties.NameProperty, StringExtensions.GetLocalized("WindowsCommunityToolkit_InAppNotification_NameProperty", "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/Microsoft.Toolkit.Uwp.UI.Controls/Resources"));
- if (ContentTemplateRoot != null)
- {
- var peer = FrameworkElementAutomationPeer.CreatePeerForElement(ContentTemplateRoot);
- if (Content?.GetType() == typeof(string))
- {
- AutomateTextNotification(peer, Content.ToString());
- }
- }
+ case StateContentVisible:
+ OnNotificationVisible();
+ break;
+ case StateContentCollapsed:
+ OnNotificationCollapsed();
+ break;
}
}
- private void AutomateTextNotification(AutomationPeer peer, string message)
+ private void OnNotificationVisible()
{
- if (peer != null)
+ Opened?.Invoke(this, EventArgs.Empty);
+ SetValue(AutomationProperties.NameProperty, StringExtensions.GetLocalized("WindowsCommunityToolkit_InAppNotification_NameProperty", "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/Microsoft.Toolkit.Uwp.UI.Controls/Resources"));
+ if (ContentTemplateRoot != null)
{
- peer.SetFocus();
- peer.RaiseNotificationEvent(
- AutomationNotificationKind.Other,
- AutomationNotificationProcessing.ImportantMostRecent,
- StringExtensions.GetLocalized("WindowsCommunityToolkit_InAppNotification_Events_NewNotificationMessage", "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/Microsoft.Toolkit.Uwp.UI.Controls/Resources") + message,
- Guid.NewGuid().ToString());
+ var peer = FrameworkElementAutomationPeer.CreatePeerForElement(ContentTemplateRoot);
+ if (Content?.GetType() == typeof(string))
+ {
+ AutomateTextNotification(peer, Content.ToString());
+ }
}
}
- private void ClosingAnimationTimer_Tick(object sender, object e)
+ private void OnNotificationCollapsed()
{
- lock (_closingAnimationTimer)
- {
- _closingAnimationTimer.Stop();
- Closed?.Invoke(this, new InAppNotificationClosedEventArgs(_lastDismissKind));
- }
+ Closed?.Invoke(this, new InAppNotificationClosedEventArgs(_lastDismissKind));
+ Visibility = Visibility.Collapsed;
}
}
}
diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.cs b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.cs
index 222c43e8723..de838e989b0 100644
--- a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.cs
+++ b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.cs
@@ -16,14 +16,14 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
[TemplateVisualState(Name = StateContentVisible, GroupName = GroupContent)]
[TemplateVisualState(Name = StateContentCollapsed, GroupName = GroupContent)]
[TemplatePart(Name = DismissButtonPart, Type = typeof(Button))]
+ [TemplatePart(Name = ContentPresenterPart, Type = typeof(ContentPresenter))]
public partial class InAppNotification : ContentControl
{
private InAppNotificationDismissKind _lastDismissKind;
- private DispatcherTimer _openAnimationTimer = new DispatcherTimer();
- private DispatcherTimer _closingAnimationTimer = new DispatcherTimer();
private DispatcherTimer _dismissTimer = new DispatcherTimer();
private Button _dismissButton;
private VisualStateGroup _visualStateGroup;
+ private ContentPresenter _contentProvider;
private List _stackedNotificationOptions = new List();
///
@@ -34,20 +34,26 @@ public InAppNotification()
DefaultStyleKey = typeof(InAppNotification);
_dismissTimer.Tick += DismissTimer_Tick;
- _openAnimationTimer.Tick += OpenAnimationTimer_Tick;
- _closingAnimationTimer.Tick += ClosingAnimationTimer_Tick;
}
///
protected override void OnApplyTemplate()
{
+ base.OnApplyTemplate();
+
if (_dismissButton != null)
{
_dismissButton.Click -= DismissButton_Click;
}
+ if (_visualStateGroup != null)
+ {
+ _visualStateGroup.CurrentStateChanged -= OnCurrentStateChanged;
+ }
+
_dismissButton = (Button)GetTemplateChild(DismissButtonPart);
_visualStateGroup = (VisualStateGroup)GetTemplateChild(GroupContent);
+ _contentProvider = (ContentPresenter)GetTemplateChild(ContentPresenterPart);
if (_dismissButton != null)
{
@@ -55,52 +61,33 @@ protected override void OnApplyTemplate()
_dismissButton.Click += DismissButton_Click;
}
- if (Visibility == Visibility.Visible)
+ if (_visualStateGroup != null)
{
- VisualStateManager.GoToState(this, StateContentVisible, true);
+ _visualStateGroup.CurrentStateChanged += OnCurrentStateChanged;
}
- else
+
+ var firstNotification = _stackedNotificationOptions.FirstOrDefault();
+ if (firstNotification != null)
{
- VisualStateManager.GoToState(this, StateContentCollapsed, true);
+ UpdateContent(firstNotification);
+ VisualStateManager.GoToState(this, StateContentVisible, true);
}
-
- base.OnApplyTemplate();
}
///
- /// Show notification using the current template
+ /// Show notification using the current content.
///
/// Displayed duration of the notification in ms (less or equal 0 means infinite duration)
public void Show(int duration = 0)
{
- lock (_openAnimationTimer)
- lock (_closingAnimationTimer)
- lock (_dismissTimer)
- {
- _openAnimationTimer.Stop();
- _closingAnimationTimer.Stop();
- _dismissTimer.Stop();
-
- var eventArgs = new InAppNotificationOpeningEventArgs();
- Opening?.Invoke(this, eventArgs);
-
- if (eventArgs.Cancel)
- {
- return;
- }
-
- Visibility = Visibility.Visible;
- VisualStateManager.GoToState(this, StateContentVisible, true);
-
- _openAnimationTimer.Interval = AnimationDuration;
- _openAnimationTimer.Start();
-
- if (duration > 0)
- {
- _dismissTimer.Interval = TimeSpan.FromMilliseconds(duration);
- _dismissTimer.Start();
- }
- }
+ // We keep our current content
+ var notificationOptions = new NotificationOptions
+ {
+ Duration = duration,
+ Content = Content
+ };
+
+ Show(notificationOptions);
}
///
@@ -134,7 +121,7 @@ public void Show(UIElement element, int duration = 0)
}
///
- /// Show notification using DataTemplate as the content of the notification
+ /// Show notification using as the content of the notification
///
/// DataTemplate used as the content of the notification
/// Displayed duration of the notification in ms (less or equal 0 means infinite duration)
@@ -148,6 +135,22 @@ public void Show(DataTemplate dataTemplate, int duration = 0)
Show(notificationOptions);
}
+ ///
+ /// Show notification using as the content of the notification.
+ /// The will be displayed with the current .
+ ///
+ /// The content of the notification
+ /// Displayed duration of the notification in ms (less or equal 0 means infinite duration)
+ public void Show(object content, int duration = 0)
+ {
+ var notificationOptions = new NotificationOptions
+ {
+ Duration = duration,
+ Content = content
+ };
+ Show(notificationOptions);
+ }
+
///
/// Dismiss the notification
///
@@ -162,64 +165,48 @@ public void Dismiss()
/// Kind of action that triggered dismiss event
private void Dismiss(InAppNotificationDismissKind dismissKind)
{
- lock (_openAnimationTimer)
- lock (_closingAnimationTimer)
- lock (_dismissTimer)
- {
- if (Visibility == Visibility.Visible)
- {
- _dismissTimer.Stop();
-
- // Continue to display notification if on remaining stacked notification
- if (_stackedNotificationOptions.Any())
- {
- _stackedNotificationOptions.RemoveAt(0);
-
- if (_stackedNotificationOptions.Any())
- {
- var notificationOptions = _stackedNotificationOptions[0];
-
- UpdateContent(notificationOptions);
-
- if (notificationOptions.Duration > 0)
- {
- _dismissTimer.Interval = TimeSpan.FromMilliseconds(notificationOptions.Duration);
- _dismissTimer.Start();
- }
-
- return;
- }
- }
-
- _openAnimationTimer.Stop();
- _closingAnimationTimer.Stop();
-
- var closingEventArgs = new InAppNotificationClosingEventArgs(dismissKind);
- Closing?.Invoke(this, closingEventArgs);
-
- if (closingEventArgs.Cancel)
- {
- return;
- }
-
- VisualStateManager.GoToState(this, StateContentCollapsed, true);
-
- _lastDismissKind = dismissKind;
-
- _closingAnimationTimer.Interval = AnimationDuration;
- _closingAnimationTimer.Start();
- }
- }
- }
+ if (_stackedNotificationOptions.Count == 0)
+ {
+ // There is nothing to dismiss.
+ return;
+ }
- ///
- /// Informs if the notification should be displayed immediately (based on the StackMode)
- ///
- /// True if notification should be displayed immediately
- private bool ShouldDisplayImmediately()
- {
- return StackMode != StackMode.QueueBehind ||
- (StackMode == StackMode.QueueBehind && _stackedNotificationOptions.Count == 0);
+ _dismissTimer.Stop();
+
+ // Continue to display notification if on remaining stacked notification
+ _stackedNotificationOptions.RemoveAt(0);
+ if (_stackedNotificationOptions.Any())
+ {
+ var notificationOptions = _stackedNotificationOptions[0];
+
+ UpdateContent(notificationOptions);
+
+ if (notificationOptions.Duration > 0)
+ {
+ _dismissTimer.Interval = TimeSpan.FromMilliseconds(notificationOptions.Duration);
+ _dismissTimer.Start();
+ }
+
+ return;
+ }
+
+ var closingEventArgs = new InAppNotificationClosingEventArgs(dismissKind);
+ Closing?.Invoke(this, closingEventArgs);
+
+ if (closingEventArgs.Cancel)
+ {
+ return;
+ }
+
+ var result = VisualStateManager.GoToState(this, StateContentCollapsed, true);
+ if (!result)
+ {
+ // The state transition cannot be executed.
+ // It means that the control's template hasn't been applied or that it doesn't contain the state.
+ Visibility = Visibility.Collapsed;
+ }
+
+ _lastDismissKind = dismissKind;
}
///
@@ -228,19 +215,29 @@ private bool ShouldDisplayImmediately()
/// Information about the notification to display
private void UpdateContent(NotificationOptions notificationOptions)
{
+ if (_contentProvider is null)
+ {
+ // The control template has not been applied yet.
+ return;
+ }
+
switch (notificationOptions.Content)
{
case string text:
- ContentTemplate = null;
- Content = text;
+ _contentProvider.ContentTemplate = null;
+ _contentProvider.Content = text;
break;
case UIElement element:
- ContentTemplate = null;
- Content = element;
+ _contentProvider.ContentTemplate = null;
+ _contentProvider.Content = element;
break;
case DataTemplate dataTemplate:
- ContentTemplate = dataTemplate;
- Content = null;
+ _contentProvider.ContentTemplate = dataTemplate;
+ _contentProvider.Content = null;
+ break;
+ case object content:
+ _contentProvider.ContentTemplate = ContentTemplate;
+ _contentProvider.Content = content;
break;
}
}
@@ -251,22 +248,48 @@ private void UpdateContent(NotificationOptions notificationOptions)
/// Information about the notification to display
private void Show(NotificationOptions notificationOptions)
{
- bool shouldDisplayImmediately = ShouldDisplayImmediately();
+ var eventArgs = new InAppNotificationOpeningEventArgs();
+ Opening?.Invoke(this, eventArgs);
- if (StackMode == StackMode.QueueBehind)
+ if (eventArgs.Cancel)
{
- _stackedNotificationOptions.Add(notificationOptions);
+ return;
}
- if (StackMode == StackMode.StackInFront)
+ var shouldDisplayImmediately = true;
+ switch (StackMode)
{
- _stackedNotificationOptions.Insert(0, notificationOptions);
+ case StackMode.Replace:
+ _stackedNotificationOptions.Clear();
+ _stackedNotificationOptions.Add(notificationOptions);
+ break;
+ case StackMode.StackInFront:
+ _stackedNotificationOptions.Insert(0, notificationOptions);
+ break;
+ case StackMode.QueueBehind:
+ _stackedNotificationOptions.Add(notificationOptions);
+ shouldDisplayImmediately = _stackedNotificationOptions.Count == 1;
+ break;
+ default:
+ break;
}
if (shouldDisplayImmediately)
{
UpdateContent(notificationOptions);
- Show(notificationOptions.Duration);
+
+ Visibility = Visibility.Visible;
+ VisualStateManager.GoToState(this, StateContentVisible, true);
+
+ if (notificationOptions.Duration > 0)
+ {
+ _dismissTimer.Interval = TimeSpan.FromMilliseconds(notificationOptions.Duration);
+ _dismissTimer.Start();
+ }
+ else
+ {
+ _dismissTimer.Stop();
+ }
}
}
}
diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.xaml b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.xaml
index 77b97ba6b58..cf0ba8c691b 100644
--- a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.xaml
+++ b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/InAppNotification.xaml
@@ -1,16 +1,16 @@
-
+
-
-
+
-
+
diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/Styles/MSEdgeNotificationStyle.xaml b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/Styles/MSEdgeNotificationStyle.xaml
index eb4d4ea550b..87b217d0d13 100644
--- a/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/Styles/MSEdgeNotificationStyle.xaml
+++ b/Microsoft.Toolkit.Uwp.UI.Controls/InAppNotification/Styles/MSEdgeNotificationStyle.xaml
@@ -1,28 +1,38 @@
-
+
-
+
-
-
-
-
+
+
+
+
-
+
-
-
-
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
-
-
+
+
-
-
-
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
+
+
-
-
-
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-