Skip to content

Commit 0b99272

Browse files
Merge branch 'master' into dev/7.0.1
2 parents 2f6f539 + cf29259 commit 0b99272

File tree

7 files changed

+272
-33
lines changed

7 files changed

+272
-33
lines changed

Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Win32AppInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static string HashAppId(string appId)
126126
private static string GetDisplayNameFromCurrentProcess(Process process)
127127
{
128128
// If AssemblyTitle is set, use that
129-
var assemblyTitleAttr = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyTitleAttribute>();
129+
var assemblyTitleAttr = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyTitleAttribute>();
130130
if (assemblyTitleAttr != null)
131131
{
132132
return assemblyTitleAttr.Title;

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
ui:ListViewExtensions.Command="{Binding SampleCommand}"
2727
ui:ListViewExtensions.AlternateColor="#33AAAAAA"
2828
ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}"
29-
ui:ListViewExtensions.StretchItemContainerDirection="Both">
29+
ui:ListViewExtensions.ItemContainerStretchDirection="Both">
3030
</ListView>
3131
</Grid>
3232
</Page>

Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static partial class ListViewExtensions
3131
/// <summary>
3232
/// Attached <see cref="DependencyProperty"/> for setting the container content stretch direction on the <see cref="Windows.UI.Xaml.Controls.ListViewBase"/>
3333
/// </summary>
34-
public static readonly DependencyProperty ItemContainerStretchDirectionProperty = DependencyProperty.RegisterAttached("ItemContainerStretchDirection", typeof(ItemContainerStretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnStretchItemContainerDirectionPropertyChanged));
34+
public static readonly DependencyProperty ItemContainerStretchDirectionProperty = DependencyProperty.RegisterAttached("ItemContainerStretchDirection", typeof(ItemContainerStretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnItemContainerStretchDirectionPropertyChanged));
3535

3636
/// <summary>
3737
/// Gets the alternate <see cref="Brush"/> associated with the specified <see cref="Windows.UI.Xaml.Controls.ListViewBase"/>
@@ -154,7 +154,7 @@ private static void ItemTemplateContainerContentChanging(Windows.UI.Xaml.Control
154154
}
155155
}
156156

157-
private static void OnStretchItemContainerDirectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
157+
private static void OnItemContainerStretchDirectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
158158
{
159159
Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase;
160160

@@ -163,17 +163,17 @@ private static void OnStretchItemContainerDirectionPropertyChanged(DependencyObj
163163
return;
164164
}
165165

166-
listViewBase.ContainerContentChanging -= StretchItemContainerDirectionChanging;
166+
listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
167167
listViewBase.Unloaded -= OnListViewBaseUnloaded;
168168

169169
if (ItemContainerStretchDirectionProperty != null)
170170
{
171-
listViewBase.ContainerContentChanging += StretchItemContainerDirectionChanging;
171+
listViewBase.ContainerContentChanging += ItemContainerStretchDirectionChanging;
172172
listViewBase.Unloaded += OnListViewBaseUnloaded;
173173
}
174174
}
175175

176-
private static void StretchItemContainerDirectionChanging(Windows.UI.Xaml.Controls.ListViewBase sender, ContainerContentChangingEventArgs args)
176+
private static void ItemContainerStretchDirectionChanging(Windows.UI.Xaml.Controls.ListViewBase sender, ContainerContentChangingEventArgs args)
177177
{
178178
var itemContainer = args.ItemContainer as SelectorItem;
179179
var stretchDirection = GetItemContainerStretchDirection(sender);
@@ -194,7 +194,7 @@ private static void OnListViewBaseUnloaded(object sender, RoutedEventArgs e)
194194
Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase;
195195
_itemsForList.Remove(listViewBase.Items);
196196

197-
listViewBase.ContainerContentChanging -= StretchItemContainerDirectionChanging;
197+
listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
198198
listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
199199
listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
200200
listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;

Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ public static Vector2 ToVector2(this string text)
2929
if (text.Length > 0)
3030
{
3131
// The format <x> or <x, y> is supported
32-
if (text.Length >= 2 &&
33-
text[0] == '>' &&
34-
text[text.Length - 1] == '>')
35-
{
36-
text = text.Substring(1, text.Length - 2);
37-
}
32+
text = Unbracket(text);
3833

3934
// Skip allocations when only a component is used
4035
if (text.IndexOf(',') == -1)
@@ -78,12 +73,7 @@ public static Vector3 ToVector3(this string text)
7873
{
7974
if (text.Length > 0)
8075
{
81-
if (text.Length >= 2 &&
82-
text[0] == '>' &&
83-
text[text.Length - 1] == '>')
84-
{
85-
text = text.Substring(1, text.Length - 2);
86-
}
76+
text = Unbracket(text);
8777

8878
if (text.IndexOf(',') == -1)
8979
{
@@ -127,12 +117,7 @@ public static Vector4 ToVector4(this string text)
127117
{
128118
if (text.Length > 0)
129119
{
130-
if (text.Length >= 2 &&
131-
text[0] == '>' &&
132-
text[text.Length - 1] == '>')
133-
{
134-
text = text.Substring(1, text.Length - 2);
135-
}
120+
text = Unbracket(text);
136121

137122
if (text.IndexOf(',') == -1)
138123
{
@@ -176,12 +161,7 @@ public static Quaternion ToQuaternion(this string text)
176161
{
177162
if (text.Length > 0)
178163
{
179-
if (text.Length >= 2 &&
180-
text[0] == '>' &&
181-
text[text.Length - 1] == '>')
182-
{
183-
text = text.Substring(1, text.Length - 2);
184-
}
164+
text = Unbracket(text);
185165

186166
string[] values = text.Split(',');
187167

@@ -201,5 +181,23 @@ public static Quaternion ToQuaternion(this string text)
201181

202182
static Quaternion Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Quaternion)}. Use the format \"float, float, float, float\"");
203183
}
184+
185+
/// <summary>
186+
/// Converts an angle bracketed <see cref="string"/> value to its unbracketed form (e.g. "&lt;float, float&gt;" to "float, float").
187+
/// If the value is already unbracketed, this method will return the value unchanged.
188+
/// </summary>
189+
/// <param name="text">A bracketed <see cref="string"/> value.</param>
190+
/// <returns>The unbracketed <see cref="string"/> value.</returns>
191+
private static string Unbracket(string text)
192+
{
193+
if (text.Length >= 2 &&
194+
text[0] == '<' &&
195+
text[text.Length - 1] == '>')
196+
{
197+
text = text.Substring(1, text.Length - 2);
198+
}
199+
200+
return text;
201+
}
204202
}
205203
}
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using System;
7+
using Microsoft.Toolkit.Uwp.UI;
8+
using System.Numerics;
9+
using System.Globalization;
10+
11+
namespace UnitTests.Extensions
12+
{
13+
[TestClass]
14+
public class Test_StringExtensions
15+
{
16+
[TestCategory("StringExtensions")]
17+
[TestMethod]
18+
[DataRow(0f)]
19+
[DataRow(3.14f)]
20+
public void Test_StringExtensions_ToVector2_XX(float x)
21+
{
22+
string text = x.ToString("R", CultureInfo.InvariantCulture);
23+
24+
Vector2
25+
expected = new(x),
26+
result = text.ToVector2();
27+
28+
Assert.AreEqual(expected, result);
29+
30+
result = $"<{text}>".ToVector2();
31+
32+
Assert.AreEqual(expected, result);
33+
}
34+
35+
[TestCategory("StringExtensions")]
36+
[TestMethod]
37+
[DataRow(0f, 0f)]
38+
[DataRow(0f, 22f)]
39+
[DataRow(3.14f, 3.24724928f)]
40+
[DataRow(float.MaxValue / 2, 0.3248f)]
41+
public void Test_StringExtensions_ToVector2_XYZW(float x, float y)
42+
{
43+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)}";
44+
45+
Vector2
46+
expected = new(x, y),
47+
result = text.ToVector2();
48+
49+
Assert.AreEqual(expected, result);
50+
51+
result = text.Replace(",", ", ").ToVector2();
52+
53+
Assert.AreEqual(expected, result);
54+
55+
result = $"<{text}>".ToVector2();
56+
57+
Assert.AreEqual(expected, result);
58+
}
59+
60+
[TestCategory("StringExtensions")]
61+
[TestMethod]
62+
[DataRow("")]
63+
[DataRow("Hello")]
64+
[DataRow("1, 2, 3")]
65+
[DataRow("<1, 2, 3")]
66+
[DataRow("<1, 2, 3>")]
67+
[DataRow("<1, 2, 3, 4>")]
68+
[ExpectedException(typeof(FormatException))]
69+
public void Test_StringExtensions_ToVector2_Fail(string text)
70+
{
71+
_ = text.ToVector2();
72+
}
73+
74+
[TestCategory("StringExtensions")]
75+
[TestMethod]
76+
[DataRow(0f)]
77+
[DataRow(3.14f)]
78+
public void Test_StringExtensions_ToVector3_XXX(float x)
79+
{
80+
string text = x.ToString("R", CultureInfo.InvariantCulture);
81+
82+
Vector3
83+
expected = new(x),
84+
result = text.ToVector3();
85+
86+
Assert.AreEqual(expected, result);
87+
88+
result = $"<{text}>".ToVector3();
89+
90+
Assert.AreEqual(expected, result);
91+
}
92+
93+
[TestCategory("StringExtensions")]
94+
[TestMethod]
95+
[DataRow(0f, 0f, 0f)]
96+
[DataRow(0f, 0f, 22f)]
97+
[DataRow(3.14f, 6.55f, 3.24724928f)]
98+
[DataRow(float.MaxValue / 2, 22f, 0.3248f)]
99+
public void Test_StringExtensions_ToVector3_XYZW(float x, float y, float z)
100+
{
101+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)},{z.ToString("R", CultureInfo.InvariantCulture)}";
102+
103+
Vector3
104+
expected = new(x, y, z),
105+
result = text.ToVector3();
106+
107+
Assert.AreEqual(expected, result);
108+
109+
result = text.Replace(",", ", ").ToVector3();
110+
111+
Assert.AreEqual(expected, result);
112+
113+
result = $"<{text}>".ToVector3();
114+
115+
Assert.AreEqual(expected, result);
116+
}
117+
118+
[TestCategory("StringExtensions")]
119+
[TestMethod]
120+
[DataRow("")]
121+
[DataRow("Hello")]
122+
[DataRow("1, 2")]
123+
[DataRow("1, 2, 3, 99")]
124+
[DataRow("<1, 2>")]
125+
[DataRow("<1, 2, 3")]
126+
[DataRow("<1, 2, 3, 4>")]
127+
[ExpectedException(typeof(FormatException))]
128+
public void Test_StringExtensions_ToVector3_Fail(string text)
129+
{
130+
_ = text.ToVector3();
131+
}
132+
133+
[TestCategory("StringExtensions")]
134+
[TestMethod]
135+
[DataRow(0f)]
136+
[DataRow(3.14f)]
137+
public void Test_StringExtensions_ToVector4_XXXX(float x)
138+
{
139+
string text = x.ToString("R", CultureInfo.InvariantCulture);
140+
141+
Vector4
142+
expected = new(x),
143+
result = text.ToVector4();
144+
145+
Assert.AreEqual(expected, result);
146+
147+
result = $"<{text}>".ToVector4();
148+
149+
Assert.AreEqual(expected, result);
150+
}
151+
152+
[TestCategory("StringExtensions")]
153+
[TestMethod]
154+
[DataRow(0f, 0f, 0f, 0f)]
155+
[DataRow(0f, 0f, 22f, 6.89f)]
156+
[DataRow(3.14f, 6.55f, 3838f, 3.24724928f)]
157+
[DataRow(float.MaxValue / 2, float.Epsilon, 22f, 0.3248f)]
158+
public void Test_StringExtensions_ToVector4_XYZW(float x, float y, float z, float w)
159+
{
160+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)},{z.ToString("R", CultureInfo.InvariantCulture)},{w.ToString("R", CultureInfo.InvariantCulture)}";
161+
162+
Vector4
163+
expected = new(x, y, z, w),
164+
result = text.ToVector4();
165+
166+
// Test the "1,2,3,4" format
167+
Assert.AreEqual(expected, result);
168+
169+
result = text.Replace(",", ", ").ToVector4();
170+
171+
// Test the "1, 2, 3, 4" format
172+
Assert.AreEqual(expected, result);
173+
174+
// Test the "<1, 2, 3, 4>" format
175+
result = $"<{text}>".ToVector4();
176+
177+
Assert.AreEqual(expected, result);
178+
}
179+
180+
[TestCategory("StringExtensions")]
181+
[TestMethod]
182+
[DataRow("")]
183+
[DataRow("Hello")]
184+
[DataRow("1, 2")]
185+
[DataRow("1, 2, 3")]
186+
[DataRow("1, 2, 3, 99, 100")]
187+
[DataRow("<1, 2, 3>")]
188+
[DataRow("<1, 2, 3, 4")]
189+
[DataRow("<1, 2, 3, 4, 5>")]
190+
[ExpectedException(typeof(FormatException))]
191+
public void Test_StringExtensions_ToVector4_Fail(string text)
192+
{
193+
_ = text.ToVector4();
194+
}
195+
196+
[TestCategory("StringExtensions")]
197+
[TestMethod]
198+
[DataRow(0f, 0f, 0f, 0f)]
199+
[DataRow(0f, 0f, 22f, 6.89f)]
200+
[DataRow(3.14f, 6.55f, 3838f, 3.24724928f)]
201+
[DataRow(float.MaxValue / 2, float.Epsilon, 22f, 0.3248f)]
202+
public void Test_StringExtensions_ToQuaternion_XYZW(float x, float y, float z, float w)
203+
{
204+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)},{z.ToString("R", CultureInfo.InvariantCulture)},{w.ToString("R", CultureInfo.InvariantCulture)}";
205+
206+
Quaternion
207+
expected = new(x, y, z, w),
208+
result = text.ToQuaternion();
209+
210+
// Test the "1,2,3,4" format
211+
Assert.AreEqual(expected, result);
212+
213+
result = text.Replace(",", ", ").ToQuaternion();
214+
215+
// Test the "1, 2, 3, 4" format
216+
Assert.AreEqual(expected, result);
217+
218+
// Test the "<1, 2, 3, 4>" format
219+
result = $"<{text}>".ToQuaternion();
220+
221+
Assert.AreEqual(expected, result);
222+
}
223+
224+
[TestCategory("StringExtensions")]
225+
[TestMethod]
226+
[DataRow("")]
227+
[DataRow("Hello")]
228+
[DataRow("1, 2")]
229+
[DataRow("1, 2, 3")]
230+
[DataRow("1, 2, 3, 99, 100")]
231+
[DataRow("<1, 2, 3>")]
232+
[DataRow("<1, 2, 3, 4")]
233+
[DataRow("<1, 2, 3, 4, 5>")]
234+
[ExpectedException(typeof(FormatException))]
235+
public void Test_StringExtensions_ToQuaternion_Fail(string text)
236+
{
237+
_ = text.ToQuaternion();
238+
}
239+
}
240+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<Compile Include="Converters\Test_StringFormatConverter.cs" />
143143
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
144144
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
145+
<Compile Include="Extensions\Test_StringExtensions.cs" />
145146
<Compile Include="Extensions\Test_VisualTreeExtensions.cs" />
146147
<Compile Include="Extensions\Test_PointExtensions.cs" />
147148
<Compile Include="Extensions\Test_RectExtensions.cs" />

0 commit comments

Comments
 (0)