Skip to content

Commit 62a3d83

Browse files
Merge branch 'master' into u/vgromfeld/bindableAttributeRemoved
2 parents a07e8ab + 8756f29 commit 62a3d83

File tree

13 files changed

+290
-11
lines changed

13 files changed

+290
-11
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<AppxBundlePlatforms>x86|x64|arm|arm64</AppxBundlePlatforms>
2222
<Win32Resource>MiddleClickScrolling-CursorType.res</Win32Resource>
2323
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
24+
<LangVersion>8.0</LangVersion>
2425
</PropertyGroup>
2526
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
2627
<DebugSymbols>true</DebugSymbols>
@@ -516,6 +517,9 @@
516517
</Content>
517518
<Content Include="SamplePages\Weibo Service\WeiboCode.bind" />
518519
<Compile Include="Common\TextBlockHyperlinkBehavior.cs" />
520+
<Compile Include="SamplePages\EnumValuesExtension\EnumValuesExtensionPage.xaml.cs">
521+
<DependentUpon>EnumValuesExtensionPage.xaml</DependentUpon>
522+
</Compile>
519523
<Compile Include="SamplePages\TilesBrush\TilesBrushPage.xaml.cs">
520524
<DependentUpon>TilesBrushPage.xaml</DependentUpon>
521525
</Compile>
@@ -617,6 +621,8 @@
617621
<Content Include="SamplePages\Triggers\UserHandPreferenceStateTrigger.bind" />
618622
<Content Include="SamplePages\Triggers\UserInteractionModeStateTrigger.bind" />
619623
<Content Include="SamplePages\StaggeredLayout\StaggeredLayout.bind" />
624+
<Content Include="SamplePages\EnumValuesExtension\EnumValuesExtensionXaml.bind" />
625+
<Content Include="SamplePages\EnumValuesExtension\EnumValuesExtensionCode.bind" />
620626
</ItemGroup>
621627
<ItemGroup>
622628
<Compile Include="App.xaml.cs">
@@ -995,6 +1001,10 @@
9951001
<SubType>Designer</SubType>
9961002
<Generator>MSBuild:Compile</Generator>
9971003
</Page>
1004+
<Page Include="SamplePages\EnumValuesExtension\EnumValuesExtensionPage.xaml">
1005+
<Generator>MSBuild:Compile</Generator>
1006+
<SubType>Designer</SubType>
1007+
</Page>
9981008
<Page Include="SamplePages\TilesBrush\TilesBrushPage.xaml">
9991009
<Generator>MSBuild:Compile</Generator>
10001010
<SubType>Designer</SubType>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Let's declare a sample enum
2+
public enum Animal
3+
{
4+
Cat,
5+
Dog,
6+
Bunny,
7+
Parrot,
8+
Squirrel
9+
}
10+
11+
// We can use a converter to get other values from our enum
12+
public sealed class AnimalToColorConverter : IValueConverter
13+
{
14+
public object Convert(object value, Type targetType, object parameter, string language)
15+
{
16+
return (Animal)value switch
17+
{
18+
Animal.Cat => Colors.Coral,
19+
Animal.Dog => Colors.Gray,
20+
Animal.Bunny => Colors.Green,
21+
Animal.Parrot => Colors.YellowGreen,
22+
Animal.Squirrel => Colors.SaddleBrown,
23+
_ => throw new ArgumentException("Invalid value", nameof(value))
24+
};
25+
}
26+
27+
public object ConvertBack(object value, Type targetType, object parameter, string language)
28+
{
29+
throw new NotImplementedException();
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.EnumValuesExtensionPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
7+
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
8+
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters"
9+
mc:Ignorable="d">
10+
<Page.Resources>
11+
<enums:Animal x:Key="MyAnimal">Cat</enums:Animal>
12+
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/>
13+
</Page.Resources>
14+
<Grid>
15+
<StackPanel Spacing="8">
16+
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"/>
17+
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
18+
SelectedIndex="0">
19+
<ComboBox.ItemTemplate>
20+
<DataTemplate>
21+
<StackPanel Orientation="Horizontal">
22+
<TextBlock Text="{Binding}"/>
23+
<Rectangle Height="16"
24+
Width="16">
25+
<Rectangle.Fill>
26+
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/>
27+
</Rectangle.Fill>
28+
</Rectangle>
29+
</StackPanel>
30+
</DataTemplate>
31+
</ComboBox.ItemTemplate>
32+
</ComboBox>
33+
</StackPanel>
34+
</Grid>
35+
</Page>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 Windows.UI;
7+
using Microsoft.Toolkit.Uwp.UI.Extensions;
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Data;
10+
using Microsoft.Toolkit.Uwp.SampleApp.Enums;
11+
12+
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
13+
{
14+
/// <summary>
15+
/// A page that shows how to use the <see cref="EnumValuesExtension"/> type.
16+
/// </summary>
17+
public sealed partial class EnumValuesExtensionPage : IXamlRenderListener
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="EnumValuesExtensionPage"/> class.
21+
/// </summary>
22+
public EnumValuesExtensionPage()
23+
{
24+
InitializeComponent();
25+
}
26+
27+
public void OnXamlRendered(FrameworkElement control)
28+
{
29+
}
30+
}
31+
}
32+
33+
#pragma warning disable SA1403 // File may only contain a single namespace
34+
namespace Microsoft.Toolkit.Uwp.SampleApp.Enums
35+
{
36+
public enum Animal
37+
{
38+
Cat,
39+
Dog,
40+
Bunny,
41+
Parrot,
42+
Squirrel
43+
}
44+
}
45+
46+
namespace Microsoft.Toolkit.Uwp.SampleApp.Converters
47+
{
48+
public sealed class AnimalToColorConverter : IValueConverter
49+
{
50+
public object Convert(object value, Type targetType, object parameter, string language)
51+
{
52+
return (Animal)value switch
53+
{
54+
Animal.Cat => Colors.Coral,
55+
Animal.Dog => Colors.Gray,
56+
Animal.Bunny => Colors.Green,
57+
Animal.Parrot => Colors.YellowGreen,
58+
Animal.Squirrel => Colors.SaddleBrown,
59+
_ => throw new ArgumentException("Invalid value", nameof(value))
60+
};
61+
}
62+
63+
public object ConvertBack(object value, Type targetType, object parameter, string language)
64+
{
65+
throw new NotImplementedException();
66+
}
67+
}
68+
}
69+
70+
#pragma warning restore SA1403
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Page
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages.EnumValuesExtensionPage"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
8+
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
9+
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters"
10+
mc:Ignorable="d">
11+
<Page.Resources>
12+
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/>
13+
</Page.Resources>
14+
<Grid>
15+
<StackPanel Spacing="8"
16+
HorizontalAlignment="Center"
17+
VerticalAlignment="Center">
18+
19+
<!--Simple selector using the default template. Each item will
20+
simply be displayed as the corresponding enum value name.-->
21+
<TextBlock Text="Select an animal:"/>
22+
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
23+
SelectedIndex="0"/>
24+
25+
<!--We can also use a custom template and some converters.-->
26+
<TextBlock Margin="0,12,0,0"
27+
Text="Pick another animal:"/>
28+
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
29+
SelectedIndex="0">
30+
<ComboBox.ItemTemplate>
31+
<DataTemplate>
32+
<Grid Width="80">
33+
<TextBlock Text="{Binding}"/>
34+
<Rectangle Height="16"
35+
Width="16"
36+
HorizontalAlignment="Right">
37+
<Rectangle.Fill>
38+
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/>
39+
</Rectangle.Fill>
40+
</Rectangle>
41+
</Grid>
42+
</DataTemplate>
43+
</ComboBox.ItemTemplate>
44+
</ComboBox>
45+
</StackPanel>
46+
</Grid>
47+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,15 +1266,23 @@
12661266
"XamlCodeFile": "OnDeviceXaml.bind",
12671267
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/OnDeviceMarkup.md"
12681268
},
1269-
{
1270-
"Name": "IconExtensions",
1271-
"Type": "IconExtensionsPage",
1272-
"About": "Markup extensions to easily create various types of icons.",
1273-
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension",
1274-
"XamlCodeFile": "IconExtensionsXaml.bind",
1275-
"Icon": "/Assets/Helpers.png",
1276-
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/IconExtensions.md"
1277-
}
1269+
{
1270+
"Name": "IconExtensions",
1271+
"Type": "IconExtensionsPage",
1272+
"About": "Markup extensions to easily create various types of icons.",
1273+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension",
1274+
"XamlCodeFile": "IconExtensionsXaml.bind",
1275+
"Icon": "/Assets/Helpers.png",
1276+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/IconExtensions.md"
1277+
},
1278+
{
1279+
"Name": "EnumValuesExtension",
1280+
"Type": "EnumValuesExtensionPage",
1281+
"About": "The EnumValuesExtension markup extension allows you to easily retrieve a collection of all values from an enum type.",
1282+
"Icon": "/Assets/Helpers.png",
1283+
"XamlCodeFile": "EnumValuesExtensionXaml.bind",
1284+
"CodeFile": "EnumValuesExtensionCode.bind"
1285+
}
12781286
]
12791287
},
12801288
{

Microsoft.Toolkit.Uwp.UI.Controls/Strings/en-US/Resources.resw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
<comment>Narrator Resource for BladeView expanded status</comment>
203203
</data>
204204
<data name="WindowsCommunityToolkit_GridSplitter_AutomationName" xml:space="preserve">
205-
<value>GridSpliter</value>
205+
<value>GridSplitter</value>
206206
<comment>Narrator Resource for GridSplitter control</comment>
207207
</data>
208208
<data name="WindowsCommunityToolkit_InAppNotification_Events_NewNotificationMessage" xml:space="preserve">
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 Windows.UI.Xaml.Markup;
7+
8+
namespace Microsoft.Toolkit.Uwp.UI.Extensions
9+
{
10+
/// <summary>
11+
/// A markup extension that returns a collection of values of a specific <see langword="enum"/>
12+
/// </summary>
13+
[MarkupExtensionReturnType(ReturnType = typeof(Array))]
14+
public sealed class EnumValuesExtension : MarkupExtension
15+
{
16+
/// <summary>
17+
/// Gets or sets the <see cref="System.Type"/> of the target <see langword="enum"/>
18+
/// </summary>
19+
public Type Type { get; set; }
20+
21+
/// <inheritdoc/>
22+
protected override object ProvideValue() => Enum.GetValues(Type);
23+
}
24+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 Microsoft.Toolkit.Uwp.UI.Extensions;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
9+
using Windows.UI.Xaml;
10+
using Windows.UI.Xaml.Controls;
11+
using Windows.UI.Xaml.Markup;
12+
13+
namespace UnitTests.Extensions
14+
{
15+
[TestClass]
16+
public class Test_EnumValuesExtension
17+
{
18+
[TestCategory("EnumValuesExtension")]
19+
[UITestMethod]
20+
public void Test_EnumValuesExtension_MarkupExtension()
21+
{
22+
var treeroot = XamlReader.Load(@"<Page
23+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
24+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
25+
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
26+
xmlns:local=""using:UnitTests.Extensions"">
27+
<ListView x:Name=""Check"" ItemsSource=""{ex:EnumValues Type=local:Animal}""/>
28+
</Page>") as FrameworkElement;
29+
30+
var list = treeroot.FindChildByName("Check") as ListView;
31+
32+
Assert.IsNotNull(list, "Could not find listview control in tree.");
33+
34+
Animal[] items = list.ItemsSource as Animal[];
35+
36+
Assert.IsNotNull(items, "The items were not created correctly");
37+
38+
CollectionAssert.AreEqual(items, Enum.GetValues(typeof(Animal)));
39+
}
40+
}
41+
42+
public enum Animal
43+
{
44+
Cat,
45+
Dog,
46+
Bunny
47+
}
48+
}

UnitTests/UnitTests.UWP/Properties/UnitTestApp.rd.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.FontIconSourceExtension" Dynamic="Required All" Serialize="Required All"/>
3636
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.SymbolIconExtension" Dynamic="Required All" Serialize="Required All"/>
3737
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.SymbolIconSourceExtension" Dynamic="Required All" Serialize="Required All"/>
38+
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.EnumValuesExtension" Dynamic="Required All" Serialize="Required All"/>
3839

3940
</Application>
4041
</Directives>

0 commit comments

Comments
 (0)