Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,29 @@ if (isTemplatePresent && inAppNotificationWithButtonsTemplate is DataTemplate)
ExampleInAppNotification.Show(inAppNotificationWithButtonsTemplate as DataTemplate);
}

// Show notification using an object
<controls:InAppNotification
x:Name="ExampleInAppNotification"
ContentTemplate="{StaticResource MyNotificationDataTemplate}" />

var notificationData = new MyNotificationData("Title", "Message");
ExampleInAppNotification.Show(notificationData, duration: 2000);

// Dismiss notification
ExampleInAppNotification.Dismiss();

// ## C# - Handle button events

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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,6 +58,15 @@ private void Load()
_exampleInAppNotification?.Show(GetRandomText(), NotificationDuration);
});

SampleController.Current.RegisterNewCommand("Show notification with object", (sender, args) =>
Comment thread
michael-hawker marked this conversation as resolved.
Resolved
{
_exampleVSCodeInAppNotification?.Dismiss();
SetDefaultControlTemplate();

var random = new Random();
_exampleInAppNotification?.Show(new KeyValuePair<int, string>(random.Next(1, 10), GetRandomText()), NotificationDuration);
});

SampleController.Current.RegisterNewCommand("Show notification with buttons (without DataTemplate)", (sender, args) =>
{
_exampleVSCodeInAppNotification?.Dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
<ContentPresenter x:Name="PART_Presenter"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
HorizontalContentAlignment="Stretch"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
VerticalContentAlignment="Center"
Expand Down Expand Up @@ -333,7 +334,8 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
<ContentPresenter x:Name="PART_Presenter"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
HorizontalContentAlignment="Stretch"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
VerticalContentAlignment="Center"
Expand Down Expand Up @@ -376,7 +378,17 @@
AnimationDuration="@[AnimationDuration:TimeSpan:100:0-5000]"
VerticalOffset="@[VerticalOffset:DoubleSlider:100.0:-200.0-200.0]"
HorizontalOffset="@[HorizontalOffset:DoubleSlider:0.0:-200.0-200.0]"
StackMode="@[StackMode:Enum:StackMode.Replace]" />
StackMode="@[StackMode:Enum:StackMode.Replace]">
<controls:InAppNotification.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Value}" Margin="0,0,0,8" />
<TextBlock Text="{Binding Key}" Style="{ThemeResource CaptionTextBlockStyle}" Opacity="0.8" />
</StackPanel>
</DataTemplate>
</controls:InAppNotification.ContentTemplate>
</controls:InAppNotification>


<controls:InAppNotification x:Name="ExampleCustomInAppNotification"
Style="{StaticResource MSEdgeNotificationTemplate_NoDismissButton}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public partial class InAppNotification
/// Key of the UI Element that dismiss the control
/// </summary>
private const string DismissButtonPart = "PART_DismissButton";

/// <summary>
/// Key of the UI Element that will display the notification content.
/// </summary>
private const string ContentPresenterPart = "PART_Presenter";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public partial class InAppNotification
/// </summary>
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", "/Microsoft.Toolkit.Uwp.UI.Controls/Resources") + message,
Guid.NewGuid().ToString());
}
}

private void DismissButton_Click(object sender, RoutedEventArgs e)
{
Dismiss(InAppNotificationDismissKind.User);
Expand All @@ -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", "/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", "/Microsoft.Toolkit.Uwp.UI.Controls/Resources"));
if (ContentTemplateRoot != null)
{
peer.SetFocus();
peer.RaiseNotificationEvent(
AutomationNotificationKind.Other,
AutomationNotificationProcessing.ImportantMostRecent,
StringExtensions.GetLocalized("WindowsCommunityToolkit_InAppNotification_Events_NewNotificationMessage", "/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;
}
}
}
Loading