Skip to content

Commit 89fe685

Browse files
Merge branch 'master' into unsetValue-to-null
2 parents b2e9725 + 2b124ff commit 89fe685

File tree

14 files changed

+355
-17
lines changed

14 files changed

+355
-17
lines changed

Microsoft.Toolkit.Uwp.Notifications/Tiles/Builder/TileContentBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public TileContentBuilder AddAdaptiveTileVisualChild(ITileBindingContentAdaptive
499499
GetAdaptiveTileContent(MediumTile).Children.Add(child);
500500
}
501501

502-
if (size.HasFlag(TileSize.Wide) && WideTile != null && GetAdaptiveTileContent(MediumTile) != null)
502+
if (size.HasFlag(TileSize.Wide) && WideTile != null && GetAdaptiveTileContent(WideTile) != null)
503503
{
504504
GetAdaptiveTileContent(WideTile).Children.Add(child);
505505
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Visual Extensions/VisualExtensionsCode.bind

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,49 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
8+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
9+
xmlns:interactions="using:Microsoft.Xaml.Interactions.Core"
10+
xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations"
11+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
812
mc:Ignorable="d">
913

10-
<Grid>
14+
<StackPanel Spacing="120" VerticalAlignment="Center">
15+
16+
<!--This is a static element, with some Visual properties being modified through
17+
the VisualExtensions class. You can modify their values to see how the position,
18+
orientation and alignment of the Border element changes.-->
1119
<Border Height="100"
1220
Width="100"
1321
Background="Purple"
22+
ui:VisualExtensions.CenterPoint="50,50,0"
23+
ui:VisualExtensions.Offset="50"
1424
ui:VisualExtensions.Opacity="0.5"
1525
ui:VisualExtensions.RotationAngleInDegrees="80"
1626
ui:VisualExtensions.Scale="2, 0.5, 1"
17-
ui:VisualExtensions.NormalizedCenterPoint="0.5" />
18-
</Grid>
27+
ui:VisualExtensions.NormalizedCenterPoint="0.5"
28+
ui:VisualExtensions.Translation="20,12,0"/>
29+
30+
<!--This Button demonstrates the VisualExtensions.Translation property in combination with a translation
31+
animation. The Translation property is set to indicate the starting position of the element (relative
32+
to its offset), and the animation will modify that to reach the specified translation value. Note how
33+
the animation doesn't have an explicit starting value, as it will just start animating the translation
34+
from the current value set via the VisualExtensions.Translation attached property.-->
35+
<Button Height="120"
36+
Width="360"
37+
Background="Green"
38+
Content="Click me!"
39+
FontSize="32"
40+
ui:VisualExtensions.Translation="20,-40,0">
41+
<ani:Explicit.Animations>
42+
<ani:AnimationSet x:Name="MoveAnimation">
43+
<ani:TranslationAnimation To="480,80,0" Duration="0:0:2"/>
44+
</ani:AnimationSet>
45+
</ani:Explicit.Animations>
46+
<interactivity:Interaction.Behaviors>
47+
<interactions:EventTriggerBehavior EventName="Click">
48+
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}"/>
49+
</interactions:EventTriggerBehavior>
50+
</interactivity:Interaction.Behaviors>
51+
</Button>
52+
</StackPanel>
1953
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Visual Extensions/VisualExtensionsPage.xaml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
6+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
7+
xmlns:interactions="using:Microsoft.Xaml.Interactions.Core"
8+
xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations"
9+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
610
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
711
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
812
mc:Ignorable="d">
913

10-
<Grid>
14+
<StackPanel Spacing="120" VerticalAlignment="Center">
1115
<Border Height="100"
1216
Width="100"
1317
Background="Purple"
@@ -16,6 +20,25 @@
1620
ui:VisualExtensions.Opacity="0.5"
1721
ui:VisualExtensions.RotationAngleInDegrees="80"
1822
ui:VisualExtensions.Scale="2, 0.5, 1"
19-
ui:VisualExtensions.NormalizedCenterPoint="0.5"/>
20-
</Grid>
23+
ui:VisualExtensions.NormalizedCenterPoint="0.5"
24+
ui:VisualExtensions.Translation="20,12,0"/>
25+
<Button
26+
Height="120"
27+
Width="360"
28+
Background="Green"
29+
Content="Click me!"
30+
FontSize="32"
31+
ui:VisualExtensions.Translation="20,-40,0">
32+
<ani:Explicit.Animations>
33+
<ani:AnimationSet x:Name="MoveAnimation">
34+
<ani:TranslationAnimation To="480,80,0" Duration="0:0:2"/>
35+
</ani:AnimationSet>
36+
</ani:Explicit.Animations>
37+
<interactivity:Interaction.Behaviors>
38+
<interactions:EventTriggerBehavior EventName="Click">
39+
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}"/>
40+
</interactions:EventTriggerBehavior>
41+
</interactivity:Interaction.Behaviors>
42+
</Button>
43+
</StackPanel>
2144
</Page>

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.Properties.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial class ColorPicker
2020
nameof(CustomPaletteColors),
2121
typeof(ObservableCollection<Windows.UI.Color>),
2222
typeof(ColorPicker),
23-
new PropertyMetadata(Windows.UI.Color.FromArgb(0x00, 0x00, 0x00, 0x00)));
23+
new PropertyMetadata(null));
2424

2525
/// <summary>
2626
/// Gets the list of custom palette colors.
@@ -64,7 +64,7 @@ public int CustomPaletteColumnCount
6464
nameof(CustomPalette),
6565
typeof(IColorPalette),
6666
typeof(ColorPicker),
67-
new PropertyMetadata(DependencyProperty.UnsetValue));
67+
new PropertyMetadata(null));
6868

6969
/// <summary>
7070
/// Gets or sets the custom color palette.

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorToColorShadeConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public object Convert(
4141
}
4242
else
4343
{
44+
// Invalid color value provided
4445
return DependencyProperty.UnsetValue;
4546
}
4647

@@ -51,6 +52,7 @@ public object Convert(
5152
}
5253
catch
5354
{
55+
// Invalid parameter provided, unable to convert to integer
5456
return DependencyProperty.UnsetValue;
5557
}
5658

@@ -120,7 +122,7 @@ public object ConvertBack(
120122
object parameter,
121123
string language)
122124
{
123-
throw new NotImplementedException();
125+
return DependencyProperty.UnsetValue;
124126
}
125127
}
126128
}

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorToHexConverter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public object Convert(
3535
}
3636
else
3737
{
38+
// Invalid color value provided
3839
return DependencyProperty.UnsetValue;
3940
}
4041

@@ -59,7 +60,8 @@ public object ConvertBack(
5960
}
6061
catch
6162
{
62-
throw new ArgumentException("Invalid hex color value provided");
63+
// Invalid hex color value provided
64+
return DependencyProperty.UnsetValue;
6365
}
6466
}
6567
else
@@ -70,7 +72,8 @@ public object ConvertBack(
7072
}
7173
catch
7274
{
73-
throw new ArgumentException("Invalid hex color value provided");
75+
// Invalid hex color value provided
76+
return DependencyProperty.UnsetValue;
7477
}
7578
}
7679
}

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ContrastBrushConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public object Convert(
4141
}
4242
else
4343
{
44+
// Invalid color value provided
4445
return DependencyProperty.UnsetValue;
4546
}
4647

@@ -82,7 +83,7 @@ public object ConvertBack(
8283
object parameter,
8384
string language)
8485
{
85-
throw new NotImplementedException();
86+
return DependencyProperty.UnsetValue;
8687
}
8788

8889
/// <summary>

Microsoft.Toolkit.Uwp.UI.Controls.Media/Microsoft.Toolkit.Uwp.UI.Controls.Media.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="Microsoft.UI.Xaml" Version="2.5.0" />
27-
<PackageReference Include="System.Text.Json" Version="4.7.2" />
27+
<PackageReference Include="System.Text.Json" Version="5.0.2" />
2828
<PackageReference Include="Win2D.uwp" Version="1.25.0" />
2929
</ItemGroup>
3030

Microsoft.Toolkit.Uwp.UI/Converters/ColorToDisplayNameConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public object Convert(
3434
}
3535
else
3636
{
37+
// Invalid color value provided
3738
return DependencyProperty.UnsetValue;
3839
}
3940

@@ -47,7 +48,7 @@ public object ConvertBack(
4748
object parameter,
4849
string language)
4950
{
50-
throw new NotImplementedException();
51+
return DependencyProperty.UnsetValue;
5152
}
5253
}
5354
}

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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.Globalization;
56
using System.Numerics;
67
using Windows.UI.Composition;
78
using Windows.UI.Xaml;
@@ -114,6 +115,36 @@ public static void SetOffset(DependencyObject obj, string value)
114115
obj.SetValue(OffsetProperty, value);
115116
}
116117

118+
/// <summary>
119+
/// Gets the <c>"Translation"</c> property of the underlying <see cref="Visual"/> object for a <see cref="UIElement"/>, in <see cref="string"/> form.
120+
/// </summary>
121+
/// <param name="obj">The <see cref="DependencyObject"/> instance.</param>
122+
/// <returns>The <see cref="string"/> representation of the <c>"Translation"</c> property property.</returns>
123+
public static string GetTranslation(DependencyObject obj)
124+
{
125+
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && obj is UIElement element)
126+
{
127+
return GetTranslationForElement(element);
128+
}
129+
130+
return (string)obj.GetValue(TranslationProperty);
131+
}
132+
133+
/// <summary>
134+
/// Sets the <c>"Translation"</c> property of the underlying <see cref="Visual"/> object for a <see cref="UIElement"/>, in <see cref="string"/> form.
135+
/// </summary>
136+
/// <param name="obj">The <see cref="DependencyObject"/> instance.</param>
137+
/// <param name="value">The <see cref="string"/> representation of the <c>"Translation"</c> property property to be set.</param>
138+
public static void SetTranslation(DependencyObject obj, string value)
139+
{
140+
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && obj is UIElement element)
141+
{
142+
SetTranslationForElement(value, element);
143+
}
144+
145+
obj.SetValue(TranslationProperty, value);
146+
}
147+
117148
/// <summary>
118149
/// Gets the <see cref="Visual.Opacity"/> of a UIElement
119150
/// </summary>
@@ -334,6 +365,12 @@ public static void SetNormalizedCenterPoint(DependencyObject obj, string value)
334365
public static readonly DependencyProperty OffsetProperty =
335366
DependencyProperty.RegisterAttached("Offset", typeof(string), typeof(VisualExtensions), new PropertyMetadata(null, OnOffsetChanged));
336367

368+
/// <summary>
369+
/// Identifies the Translation attached property.
370+
/// </summary>
371+
public static readonly DependencyProperty TranslationProperty =
372+
DependencyProperty.RegisterAttached("Translation", typeof(string), typeof(VisualExtensions), new PropertyMetadata(null, OnTranslationChanged));
373+
337374
/// <summary>
338375
/// Identifies the Opacity attached property.
339376
/// </summary>
@@ -400,6 +437,14 @@ private static void OnOffsetChanged(DependencyObject d, DependencyPropertyChange
400437
}
401438
}
402439

440+
private static void OnTranslationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
441+
{
442+
if (e.NewValue is string str)
443+
{
444+
SetTranslation(d, str);
445+
}
446+
}
447+
403448
private static void OnOpacityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
404449
{
405450
if (e.NewValue is double dbl)
@@ -503,6 +548,37 @@ private static void SetOffsetForElement(string value, UIElement element)
503548
visual.Offset = value.ToVector3();
504549
}
505550

551+
private static string GetTranslationForElement(UIElement element)
552+
{
553+
CompositionGetValueStatus result = GetVisual(element).Properties.TryGetVector3("Translation", out Vector3 translation);
554+
555+
return result switch
556+
{
557+
// The ("G", CultureInfo.InvariantCulture) combination produces a string with the default numeric
558+
// formatting style, and using ',' as component separator, so that the resulting text can safely
559+
// be parsed back if needed with the StringExtensions.ToVector3(string) extension, which uses
560+
// the invariant culture mode by default so that the syntax will always match that from XAML.
561+
CompositionGetValueStatus.Succeeded => translation.ToString("G", CultureInfo.InvariantCulture),
562+
_ => "<0, 0, 0>"
563+
};
564+
}
565+
566+
private static void SetTranslationForElement(string value, UIElement element)
567+
{
568+
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
569+
570+
// The "Translation" attached property refers to the "hidden" property that is enabled
571+
// through "ElementCompositionPreview.SetIsTranslationEnabled". The value for this property
572+
// is not available directly on the Visual class and can only be accessed through its property
573+
// set. Note that this "Translation" value is not the same as Visual.TransformMatrix.Translation.
574+
// In fact, the latter doesn't require to be explicitly enabled and is actually combined with
575+
// this at runtime (ie. the whole transform matrix is combined with the additional translation
576+
// from the "Translation" property, if any), and the two can be set and animated independently.
577+
// In this case we're just interested in the "Translation" property, which is more commonly used
578+
// as it can also be animated directly with a Vector3 animation instead of a Matrix4x4 one.
579+
GetVisual(element).Properties.InsertVector3("Translation", value.ToVector3());
580+
}
581+
506582
private static double GetOpacityForElement(UIElement element)
507583
{
508584
var visual = GetVisual(element);

0 commit comments

Comments
 (0)