Skip to content

Commit f7bf580

Browse files
Merge branch 'master' into fix-local-release-build
2 parents 1217466 + 4dc90dc commit f7bf580

32 files changed

+820
-513
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
// This extension is restricted to the .NET 5 because it shares the same BCL
6+
// across all targets, ensuring that the layout of our Nullable<T> mapping type
7+
// will be correct. Exposing this API on older targets (especially .NET Standard)
8+
// is not guaranteed to be correct and could result in invalid memory accesses.
9+
#if NET5_0
10+
11+
using System;
12+
using System.Runtime.CompilerServices;
13+
14+
namespace Microsoft.Toolkit.HighPerformance.Extensions
15+
{
16+
/// <summary>
17+
/// Helpers for working with the <see cref="Nullable{T}"/> type.
18+
/// </summary>
19+
public static class NullableExtensions
20+
{
21+
/// <summary>
22+
/// Returns a reference to the value of the input <see cref="Nullable{T}"/> instance, regardless of whether
23+
/// the <see cref="Nullable{T}.HasValue"/> property is returning <see langword="true"/> or not. If that is not
24+
/// the case, this method will still return a reference to the underlying <see langword="default"/> value.
25+
/// </summary>
26+
/// <typeparam name="T">The type of the underlying value</typeparam>
27+
/// <param name="value">The <see cref="Nullable{T}"/></param>
28+
/// <returns>A reference to the underlying value from the input <see cref="Nullable{T}"/> instance.</returns>
29+
/// <remarks>
30+
/// Note that attempting to mutate the returned reference will not change the value returned by <see cref="Nullable{T}.HasValue"/>.
31+
/// That means that reassigning the value of an empty instance will not make <see cref="Nullable{T}.HasValue"/> return <see langword="true"/>.
32+
/// </remarks>
33+
public static ref T DangerousGetValueOrDefaultReference<T>(this ref T? value)
34+
where T : struct
35+
{
36+
return ref Unsafe.As<T?, RawNullableData<T>>(ref value).Value;
37+
}
38+
39+
/// <summary>
40+
/// Mapping type that reflects the internal layout of the <see cref="Nullable{T}"/> type.
41+
/// See https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Nullable.cs.
42+
/// </summary>
43+
/// <typeparam name="T">The value type wrapped by the current instance.</typeparam>
44+
private struct RawNullableData<T>
45+
where T : struct
46+
{
47+
#pragma warning disable CS0649 // Unassigned fields
48+
public bool HasValue;
49+
public T Value;
50+
#pragma warning restore CS0649
51+
}
52+
}
53+
}
54+
55+
#endif

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

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ 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;
@@ -36,9 +34,7 @@ public void OnXamlRendered(FrameworkElement control)
3634
NotificationDuration = 0;
3735

3836
_exampleInAppNotification = control.FindChild("ExampleInAppNotification") as InAppNotification;
39-
_defaultInAppNotificationControlTemplate = _exampleInAppNotification?.Template;
4037
_exampleCustomInAppNotification = control.FindChild("ExampleCustomInAppNotification") as InAppNotification;
41-
_customInAppNotificationControlTemplate = _exampleCustomInAppNotification?.Template;
4238
_exampleVSCodeInAppNotification = control.FindChild("ExampleVSCodeInAppNotification") as InAppNotification;
4339
_resources = control.Resources;
4440

@@ -53,24 +49,24 @@ private void Load()
5349
{
5450
SampleController.Current.RegisterNewCommand("Show notification with random text", (sender, args) =>
5551
{
56-
_exampleVSCodeInAppNotification?.Dismiss();
57-
SetDefaultControlTemplate();
52+
_exampleVSCodeInAppNotification.Dismiss(true);
53+
_exampleCustomInAppNotification.Dismiss(true);
5854
_exampleInAppNotification?.Show(GetRandomText(), NotificationDuration);
5955
});
6056

6157
SampleController.Current.RegisterNewCommand("Show notification with object", (sender, args) =>
6258
{
63-
_exampleVSCodeInAppNotification?.Dismiss();
64-
SetDefaultControlTemplate();
59+
_exampleVSCodeInAppNotification.Dismiss(true);
60+
_exampleCustomInAppNotification.Dismiss(true);
6561

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

7066
SampleController.Current.RegisterNewCommand("Show notification with buttons (without DataTemplate)", (sender, args) =>
7167
{
72-
_exampleVSCodeInAppNotification?.Dismiss();
73-
SetDefaultControlTemplate();
68+
_exampleVSCodeInAppNotification.Dismiss(true);
69+
_exampleCustomInAppNotification.Dismiss(true);
7470

7571
var grid = new Grid()
7672
{
@@ -126,46 +122,30 @@ private void Load()
126122

127123
SampleController.Current.RegisterNewCommand("Show notification with buttons (with DataTemplate)", (sender, args) =>
128124
{
129-
_exampleVSCodeInAppNotification?.Dismiss();
130-
SetCustomControlTemplate(); // Use the custom template without the Dismiss button. The DataTemplate will handle re-adding it.
125+
_exampleVSCodeInAppNotification.Dismiss(true);
126+
_exampleInAppNotification.Dismiss(true);
131127

132-
object inAppNotificationWithButtonsTemplate = null;
133-
bool? isTemplatePresent = _resources?.TryGetValue("InAppNotificationWithButtonsTemplate", out inAppNotificationWithButtonsTemplate);
134-
135-
if (isTemplatePresent == true && inAppNotificationWithButtonsTemplate is DataTemplate template)
128+
object inAppNotificationWithButtonsTemplateResource = null;
129+
bool? isTemplatePresent = _resources?.TryGetValue("InAppNotificationWithButtonsTemplate", out inAppNotificationWithButtonsTemplateResource);
130+
if (isTemplatePresent == true && inAppNotificationWithButtonsTemplateResource is DataTemplate inAppNotificationWithButtonsTemplate)
136131
{
137-
_exampleInAppNotification.Show(template, NotificationDuration);
132+
_exampleCustomInAppNotification.Show(inAppNotificationWithButtonsTemplate, NotificationDuration);
138133
}
139134
});
140135

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);
156-
});
157-
158136
SampleController.Current.RegisterNewCommand("Show notification with Visual Studio Code template (info notification)", (sender, args) =>
159137
{
160-
_exampleInAppNotification.Dismiss();
138+
_exampleInAppNotification.Dismiss(true);
139+
_exampleCustomInAppNotification.Dismiss(true);
161140
_exampleVSCodeInAppNotification.Show(NotificationDuration);
162141
});
163142

164143
SampleController.Current.RegisterNewCommand("Dismiss", (sender, args) =>
165144
{
166145
// Dismiss all notifications (should not be replicated in production)
167-
_exampleInAppNotification.Dismiss();
168-
_exampleVSCodeInAppNotification.Dismiss();
146+
_exampleInAppNotification.Dismiss(true);
147+
_exampleCustomInAppNotification.Dismiss(true);
148+
_exampleVSCodeInAppNotification.Dismiss(true);
169149
});
170150
}
171151

@@ -182,18 +162,6 @@ private string GetRandomText()
182162
}
183163
}
184164

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-
197165
private void NotificationDurationTextBox_TextChanged(object sender, TextChangedEventArgs e)
198166
{
199167
int newDuration;

0 commit comments

Comments
 (0)