Skip to content

Commit 3916d5a

Browse files
committed
add show(object)
1 parent cdf0d09 commit 3916d5a

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationPage.xaml.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Windows.Input;
75
using Microsoft.Toolkit.Uwp.UI.Controls;
86
using Microsoft.Toolkit.Uwp.UI.Extensions;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Windows.Input;
910
using Windows.UI.Xaml;
1011
using Windows.UI.Xaml.Controls;
1112

@@ -57,6 +58,15 @@ private void Load()
5758
_exampleInAppNotification?.Show(GetRandomText(), NotificationDuration);
5859
});
5960

61+
SampleController.Current.RegisterNewCommand("Show notification with object", (sender, args) =>
62+
{
63+
_exampleVSCodeInAppNotification?.Dismiss();
64+
SetDefaultControlTemplate();
65+
66+
var random = new Random();
67+
_exampleInAppNotification?.Show(new KeyValuePair<int, string>(random.Next(1, 10), GetRandomText()), NotificationDuration);
68+
});
69+
6070
SampleController.Current.RegisterNewCommand("Show notification with buttons (without DataTemplate)", (sender, args) =>
6171
{
6272
_exampleVSCodeInAppNotification?.Dismiss();

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/InAppNotification/InAppNotificationXaml.bind

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,17 @@
378378
AnimationDuration="@[AnimationDuration:TimeSpan:100:0-5000]"
379379
VerticalOffset="@[VerticalOffset:DoubleSlider:100.0:-200.0-200.0]"
380380
HorizontalOffset="@[HorizontalOffset:DoubleSlider:0.0:-200.0-200.0]"
381-
StackMode="@[StackMode:Enum:StackMode.Replace]" />
381+
StackMode="@[StackMode:Enum:StackMode.Replace]">
382+
<controls:InAppNotification.ContentTemplate>
383+
<DataTemplate>
384+
<StackPanel>
385+
<TextBlock Text="{Binding Value}" Margin="0,0,0,8" />
386+
<TextBlock Text="{Binding Key}" Style="{ThemeResource CaptionTextBlockStyle}" Opacity="0.8" />
387+
</StackPanel>
388+
</DataTemplate>
389+
</controls:InAppNotification.ContentTemplate>
390+
</controls:InAppNotification>
391+
382392

383393
<controls:InAppNotification x:Name="ExampleCustomInAppNotification"
384394
Style="{StaticResource MSEdgeNotificationTemplate_NoDismissButton}"

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected override void OnApplyTemplate()
7575
}
7676

7777
/// <summary>
78-
/// Show notification using the current template
78+
/// Show notification using the current content.
7979
/// </summary>
8080
/// <param name="duration">Displayed duration of the notification in ms (less or equal 0 means infinite duration)</param>
8181
public void Show(int duration = 0)
@@ -131,7 +131,7 @@ public void Show(UIElement element, int duration = 0)
131131
}
132132

133133
/// <summary>
134-
/// Show notification using DataTemplate as the content of the notification
134+
/// Show notification using <paramref name="dataTemplate"/> as the content of the notification
135135
/// </summary>
136136
/// <param name="dataTemplate">DataTemplate used as the content of the notification</param>
137137
/// <param name="duration">Displayed duration of the notification in ms (less or equal 0 means infinite duration)</param>
@@ -145,6 +145,22 @@ public void Show(DataTemplate dataTemplate, int duration = 0)
145145
Show(notificationOptions);
146146
}
147147

148+
/// <summary>
149+
/// Show notification using <paramref name="content"/> as the content of the notification.
150+
/// The <paramref name="content"/> will be displayed with the current <see cref="ContentControl.ContentTemplate"/>.
151+
/// </summary>
152+
/// <param name="content">The content of the notification</param>
153+
/// <param name="duration">Displayed duration of the notification in ms (less or equal 0 means infinite duration)</param>
154+
public void Show(object content, int duration = 0)
155+
{
156+
var notificationOptions = new NotificationOptions
157+
{
158+
Duration = duration,
159+
Content = content
160+
};
161+
Show(notificationOptions);
162+
}
163+
148164
/// <summary>
149165
/// Dismiss the notification
150166
/// </summary>
@@ -229,6 +245,10 @@ private void UpdateContent(NotificationOptions notificationOptions)
229245
_contentProvider.ContentTemplate = dataTemplate;
230246
_contentProvider.Content = null;
231247
break;
248+
case object content:
249+
_contentProvider.ContentTemplate = ContentTemplate;
250+
_contentProvider.Content = content;
251+
break;
232252
}
233253
}
234254

0 commit comments

Comments
 (0)