Skip to content

Commit b66de95

Browse files
committed
Restructuring use of custom notification
1 parent 6f256a1 commit b66de95

File tree

3 files changed

+147
-268
lines changed

3 files changed

+147
-268
lines changed

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

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
1414
{
1515
public sealed partial class InAppNotificationPage : Page, IXamlRenderListener
1616
{
17-
private ControlTemplate _defaultInAppNotificationControlTemplate;
18-
private ControlTemplate _customInAppNotificationControlTemplate;
1917
private InAppNotification _exampleInAppNotification;
2018
private InAppNotification _exampleCustomInAppNotification;
2119
private InAppNotification _exampleVSCodeInAppNotification;
20+
private DataTemplate _inAppNotificationWithButtonsTemplate;
2221
private ResourceDictionary _resources;
2322

2423
public bool IsRootGridActualWidthLargerThan700 { get; set; }
@@ -36,9 +35,7 @@ public void OnXamlRendered(FrameworkElement control)
3635
NotificationDuration = 0;
3736

3837
_exampleInAppNotification = control.FindChild("ExampleInAppNotification") as InAppNotification;
39-
_defaultInAppNotificationControlTemplate = _exampleInAppNotification?.Template;
4038
_exampleCustomInAppNotification = control.FindChild("ExampleCustomInAppNotification") as InAppNotification;
41-
_customInAppNotificationControlTemplate = _exampleCustomInAppNotification?.Template;
4239
_exampleVSCodeInAppNotification = control.FindChild("ExampleVSCodeInAppNotification") as InAppNotification;
4340
_resources = control.Resources;
4441

@@ -47,30 +44,37 @@ public void OnXamlRendered(FrameworkElement control)
4744
{
4845
notificationDurationTextBox.TextChanged += NotificationDurationTextBox_TextChanged;
4946
}
47+
48+
object inAppNotificationWithButtonsTemplateResource = null;
49+
bool? isTemplatePresent = _resources?.TryGetValue("InAppNotificationWithButtonsTemplate", out inAppNotificationWithButtonsTemplateResource);
50+
if (isTemplatePresent == true && inAppNotificationWithButtonsTemplateResource is DataTemplate inAppNotificationWithButtonsTemplate)
51+
{
52+
_inAppNotificationWithButtonsTemplate = inAppNotificationWithButtonsTemplate;
53+
}
5054
}
5155

5256
private void Load()
5357
{
5458
SampleController.Current.RegisterNewCommand("Show notification with random text", (sender, args) =>
5559
{
56-
_exampleVSCodeInAppNotification?.Dismiss();
57-
SetDefaultControlTemplate();
60+
_exampleVSCodeInAppNotification.Dismiss(true);
61+
_exampleCustomInAppNotification.Dismiss(true);
5862
_exampleInAppNotification?.Show(GetRandomText(), NotificationDuration);
5963
});
6064

6165
SampleController.Current.RegisterNewCommand("Show notification with object", (sender, args) =>
6266
{
63-
_exampleVSCodeInAppNotification?.Dismiss();
64-
SetDefaultControlTemplate();
67+
_exampleVSCodeInAppNotification.Dismiss(true);
68+
_exampleCustomInAppNotification.Dismiss(true);
6569

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

7074
SampleController.Current.RegisterNewCommand("Show notification with buttons (without DataTemplate)", (sender, args) =>
7175
{
72-
_exampleVSCodeInAppNotification?.Dismiss();
73-
SetDefaultControlTemplate();
76+
_exampleVSCodeInAppNotification.Dismiss(true);
77+
_exampleCustomInAppNotification.Dismiss(true);
7478

7579
var grid = new Grid()
7680
{
@@ -126,46 +130,24 @@ private void Load()
126130

127131
SampleController.Current.RegisterNewCommand("Show notification with buttons (with DataTemplate)", (sender, args) =>
128132
{
129-
_exampleVSCodeInAppNotification?.Dismiss();
130-
SetCustomControlTemplate(); // Use the custom template without the Dismiss button. The DataTemplate will handle re-adding it.
131-
132-
object inAppNotificationWithButtonsTemplate = null;
133-
bool? isTemplatePresent = _resources?.TryGetValue("InAppNotificationWithButtonsTemplate", out inAppNotificationWithButtonsTemplate);
134-
135-
if (isTemplatePresent == true && inAppNotificationWithButtonsTemplate is DataTemplate template)
136-
{
137-
_exampleInAppNotification.Show(template, NotificationDuration);
138-
}
139-
});
140-
141-
SampleController.Current.RegisterNewCommand("Show notification with Drop Shadow (based on default template)", (sender, args) =>
142-
{
143-
_exampleVSCodeInAppNotification.Dismiss();
144-
SetDefaultControlTemplate();
145-
146-
// Update control template
147-
object inAppNotificationDropShadowControlTemplate = null;
148-
bool? isTemplatePresent = _resources?.TryGetValue("InAppNotificationDropShadowControlTemplate", out inAppNotificationDropShadowControlTemplate);
149-
150-
if (isTemplatePresent == true && inAppNotificationDropShadowControlTemplate is ControlTemplate template)
151-
{
152-
_exampleInAppNotification.Template = template;
153-
}
154-
155-
_exampleInAppNotification.Show(GetRandomText(), NotificationDuration);
133+
_exampleVSCodeInAppNotification.Dismiss(true);
134+
_exampleInAppNotification.Dismiss(true);
135+
_exampleCustomInAppNotification.Show(_inAppNotificationWithButtonsTemplate, NotificationDuration);
156136
});
157137

158138
SampleController.Current.RegisterNewCommand("Show notification with Visual Studio Code template (info notification)", (sender, args) =>
159139
{
160-
_exampleInAppNotification.Dismiss();
140+
_exampleInAppNotification.Dismiss(true);
141+
_exampleCustomInAppNotification.Dismiss(true);
161142
_exampleVSCodeInAppNotification.Show(NotificationDuration);
162143
});
163144

164145
SampleController.Current.RegisterNewCommand("Dismiss", (sender, args) =>
165146
{
166147
// Dismiss all notifications (should not be replicated in production)
167-
_exampleInAppNotification.Dismiss();
168-
_exampleVSCodeInAppNotification.Dismiss();
148+
_exampleInAppNotification.Dismiss(true);
149+
_exampleCustomInAppNotification.Dismiss(true);
150+
_exampleVSCodeInAppNotification.Dismiss(true);
169151
});
170152
}
171153

@@ -182,18 +164,6 @@ private string GetRandomText()
182164
}
183165
}
184166

185-
private void SetDefaultControlTemplate()
186-
{
187-
// Update control template
188-
_exampleInAppNotification.Template = _defaultInAppNotificationControlTemplate;
189-
}
190-
191-
private void SetCustomControlTemplate()
192-
{
193-
// Update control template
194-
_exampleInAppNotification.Template = _customInAppNotificationControlTemplate;
195-
}
196-
197167
private void NotificationDurationTextBox_TextChanged(object sender, TextChangedEventArgs e)
198168
{
199169
int newDuration;

0 commit comments

Comments
 (0)