Skip to content

Commit 72d524c

Browse files
Add Vector3 support for Sample Bindings
Add additional shortcuts/fallbacks for parsing other Vector formats to different types
1 parent f2a8254 commit 72d524c

File tree

7 files changed

+103
-16
lines changed

7 files changed

+103
-16
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 System;
6+
using System.Linq;
7+
using System.Numerics;
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Data;
10+
11+
namespace Microsoft.Toolkit.Uwp.SampleApp.Common
12+
{
13+
public class Vector3Converter : IValueConverter
14+
{
15+
public object Convert(object value, Type targetType, object parameter, string language)
16+
{
17+
if (value is string)
18+
{
19+
return value;
20+
}
21+
22+
var thickness = (Vector3)value;
23+
24+
return thickness.ToString().TrimStart('<').Replace(" ", string.Empty).TrimEnd('>');
25+
}
26+
27+
public object ConvertBack(object value, Type targetType, object parameter, string language)
28+
{
29+
if (value is string vectorString)
30+
{
31+
var vectorTokens = vectorString.Split(',')
32+
.Where(tkn => !string.IsNullOrWhiteSpace(tkn))
33+
.ToArray();
34+
switch (vectorTokens.Length)
35+
{
36+
case 1:
37+
var vectorValue = float.Parse(vectorString);
38+
return new Vector3(vectorValue, vectorValue, vectorValue);
39+
case 2:
40+
var xValue = float.Parse(vectorTokens[0]);
41+
var yValue = float.Parse(vectorTokens[1]);
42+
43+
return new Vector3(xValue, yValue, 0);
44+
case 3:
45+
return new Vector3(
46+
float.Parse(vectorTokens[0]),
47+
float.Parse(vectorTokens[1]),
48+
float.Parse(vectorTokens[2]));
49+
default:
50+
return default(Vector3);
51+
}
52+
}
53+
54+
return value.ToString();
55+
}
56+
}
57+
}

Microsoft.Toolkit.Uwp.SampleApp/Controls/PropertyControl.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ private void PropertyControl_OnDataContextChanged(FrameworkElement sender, DataC
179179
converter = new ThicknessConverter();
180180
break;
181181

182+
case PropertyKind.Vector3:
183+
var vectorTextBox = new TextBox { Text = (propertyDict[option.Name] as ValueHolder).Value.ToString() };
184+
185+
controlToAdd = vectorTextBox;
186+
dependencyProperty = TextBox.TextProperty;
187+
converter = new Vector3Converter();
188+
break;
189+
182190
default:
183191
var textBox = new TextBox { Text = (propertyDict[option.Name] as ValueHolder).Value.ToString() };
184192

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@
488488
<Content Include="SamplePages\DataGrid\DataGridCode.bind" />
489489
<Content Include="SamplePages\ViewportBehavior\ViewportBehaviorCode.bind" />
490490
<Compile Include="Common\TextBlockHyperlinkBehavior.cs" />
491+
<Compile Include="Common\Vector3Converter.cs" />
491492
<Compile Include="SamplePages\AutoFocusBehavior\AutoFocusBehaviorPage.xaml.cs">
492493
<DependentUpon>AutoFocusBehaviorPage.xaml</DependentUpon>
493494
</Compile>
@@ -663,7 +664,6 @@
663664
<Compile Include="SamplePages\UniformGrid\UniformGridPage.xaml.cs">
664665
<DependentUpon>UniformGridPage.xaml</DependentUpon>
665666
</Compile>
666-
<Compile Include="Models\PropertyDescriptor\ThicknessPropertyOptions.cs" />
667667
<Compile Include="Models\ThemeChangedArgs.cs" />
668668
<Compile Include="Pages\SampleController.xaml.cs">
669669
<DependentUpon>SampleController.xaml</DependentUpon>

Microsoft.Toolkit.Uwp.SampleApp/Models/PropertyDescriptor/PropertyKind.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum PropertyKind
1313
Bool,
1414
Brush,
1515
TimeSpan,
16-
Thickness
16+
Thickness,
17+
Vector3,
1718
}
1819
}

Microsoft.Toolkit.Uwp.SampleApp/Models/PropertyDescriptor/ThicknessPropertyOptions.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,12 @@ public string UpdatedXamlCode
418418
{
419419
if (proxy[option.Name] is ValueHolder value)
420420
{
421-
var newString = value.Value is Windows.UI.Xaml.Media.SolidColorBrush brush ?
422-
brush.Color.ToString() : value.Value.ToString();
421+
var newString = value.Value switch
422+
{
423+
Windows.UI.Xaml.Media.SolidColorBrush brush => brush.Color.ToString(),
424+
System.Numerics.Vector3 vector => vector.ToString().TrimStart('<').Replace(" ", string.Empty).TrimEnd('>'),
425+
_ => value.Value.ToString()
426+
};
423427

424428
result = result.Replace(option.OriginalString, newString);
425429
result = result.Replace("@[" + option.Label + "]@", newString);
@@ -630,12 +634,27 @@ public async Task PreparePropertyDescriptorAsync()
630634
case PropertyKind.Thickness:
631635
try
632636
{
633-
var thicknessOptions = new ThicknessPropertyOptions { DefaultValue = value };
637+
var thicknessOptions = new PropertyOptions { DefaultValue = value };
634638
options = thicknessOptions;
635639
}
636640
catch (Exception ex)
637641
{
638-
Debug.WriteLine($"Unable to extract slider info from {value}({ex.Message})");
642+
Debug.WriteLine($"Unable to extract thickness info from {value}({ex.Message})");
643+
TrackingManager.TrackException(ex);
644+
continue;
645+
}
646+
647+
break;
648+
649+
case PropertyKind.Vector3:
650+
try
651+
{
652+
var vector3Options = new PropertyOptions { DefaultValue = value };
653+
options = vector3Options;
654+
}
655+
catch (Exception ex)
656+
{
657+
Debug.WriteLine($"Unable to extract vector3 info from {value}({ex.Message})");
639658
TrackingManager.TrackException(ex);
640659
continue;
641660
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public static Vector3 ToVector3(this string text)
9595
return new(x, y, z);
9696
}
9797
}
98+
else if (values.Length == 2)
99+
{
100+
return new(text.ToVector2(), 0);
101+
}
98102
}
99103
}
100104

@@ -140,6 +144,14 @@ public static Vector4 ToVector4(this string text)
140144
return new(x, y, z, w);
141145
}
142146
}
147+
else if (values.Length == 3)
148+
{
149+
return new(text.ToVector3(), 0);
150+
}
151+
else if (values.Length == 2)
152+
{
153+
return new(text.ToVector2(), 0, 0);
154+
}
143155
}
144156
}
145157

0 commit comments

Comments
 (0)