Skip to content

Commit e6d29c8

Browse files
committed
Updated InAppNotification to use new DispatcherQueue
1 parent 5932530 commit e6d29c8

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Core/InAppNotification/InAppNotification.cs

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using Windows.System;
89
using Windows.UI.Xaml;
910
using Windows.UI.Xaml.Automation;
1011
using Windows.UI.Xaml.Controls;
@@ -20,12 +21,13 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
2021
[TemplatePart(Name = ContentPresenterPart, Type = typeof(ContentPresenter))]
2122
public partial class InAppNotification : ContentControl
2223
{
23-
private InAppNotificationDismissKind _lastDismissKind;
24-
private DispatcherTimer _dismissTimer = new DispatcherTimer();
24+
private ContentPresenter _contentProvider;
25+
private DispatcherQueueTimer _dismissTimer;
2526
private Button _dismissButton;
27+
private DispatcherQueue _dispatcherQueue;
28+
private InAppNotificationDismissKind _lastDismissKind;
29+
private List<NotificationOptions> _stackedNotificationOptions;
2630
private VisualStateGroup _visualStateGroup;
27-
private ContentPresenter _contentProvider;
28-
private List<NotificationOptions> _stackedNotificationOptions = new List<NotificationOptions>();
2931

3032
/// <summary>
3133
/// Initializes a new instance of the <see cref="InAppNotification"/> class.
@@ -34,7 +36,11 @@ public InAppNotification()
3436
{
3537
DefaultStyleKey = typeof(InAppNotification);
3638

39+
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
40+
_dismissTimer = _dispatcherQueue.CreateTimer();
3741
_dismissTimer.Tick += DismissTimer_Tick;
42+
43+
_stackedNotificationOptions = new List<NotificationOptions>();
3844
}
3945

4046
/// <inheritdoc />
@@ -274,52 +280,49 @@ private void UpdateContent(NotificationOptions notificationOptions)
274280
/// <param name="notificationOptions">Information about the notification to display</param>
275281
private async void Show(NotificationOptions notificationOptions)
276282
{
277-
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
283+
var eventArgs = new InAppNotificationOpeningEventArgs();
284+
Opening?.Invoke(this, eventArgs);
285+
286+
if (eventArgs.Cancel)
278287
{
279-
var eventArgs = new InAppNotificationOpeningEventArgs();
280-
Opening?.Invoke(this, eventArgs);
288+
return;
289+
}
281290

282-
if (eventArgs.Cancel)
283-
{
284-
return;
285-
}
291+
var shouldDisplayImmediately = true;
292+
switch (StackMode)
293+
{
294+
case StackMode.Replace:
295+
_stackedNotificationOptions.Clear();
296+
_stackedNotificationOptions.Add(notificationOptions);
297+
break;
298+
case StackMode.StackInFront:
299+
_stackedNotificationOptions.Insert(0, notificationOptions);
300+
break;
301+
case StackMode.QueueBehind:
302+
_stackedNotificationOptions.Add(notificationOptions);
303+
shouldDisplayImmediately = _stackedNotificationOptions.Count == 1;
304+
break;
305+
default:
306+
break;
307+
}
286308

287-
var shouldDisplayImmediately = true;
288-
switch (StackMode)
309+
if (shouldDisplayImmediately)
310+
{
311+
Visibility = Visibility.Visible;
312+
VisualStateManager.GoToState(this, StateContentVisible, true);
313+
314+
UpdateContent(notificationOptions);
315+
316+
if (notificationOptions.Duration > 0)
289317
{
290-
case StackMode.Replace:
291-
_stackedNotificationOptions.Clear();
292-
_stackedNotificationOptions.Add(notificationOptions);
293-
break;
294-
case StackMode.StackInFront:
295-
_stackedNotificationOptions.Insert(0, notificationOptions);
296-
break;
297-
case StackMode.QueueBehind:
298-
_stackedNotificationOptions.Add(notificationOptions);
299-
shouldDisplayImmediately = _stackedNotificationOptions.Count == 1;
300-
break;
301-
default:
302-
break;
318+
_dismissTimer.Interval = TimeSpan.FromMilliseconds(notificationOptions.Duration);
319+
_dismissTimer.Start();
303320
}
304-
305-
if (shouldDisplayImmediately)
321+
else
306322
{
307-
Visibility = Visibility.Visible;
308-
VisualStateManager.GoToState(this, StateContentVisible, true);
309-
310-
UpdateContent(notificationOptions);
311-
312-
if (notificationOptions.Duration > 0)
313-
{
314-
_dismissTimer.Interval = TimeSpan.FromMilliseconds(notificationOptions.Duration);
315-
_dismissTimer.Start();
316-
}
317-
else
318-
{
319-
_dismissTimer.Stop();
320-
}
323+
_dismissTimer.Stop();
321324
}
322-
});
325+
}
323326
}
324327
}
325328
}

0 commit comments

Comments
 (0)