Skip to content

Enum values markdown extension #3357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<AppxBundlePlatforms>x86|x64|arm|arm64</AppxBundlePlatforms>
<Win32Resource>MiddleClickScrolling-CursorType.res</Win32Resource>
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -516,6 +517,9 @@
</Content>
<Content Include="SamplePages\Weibo Service\WeiboCode.bind" />
<Compile Include="Common\TextBlockHyperlinkBehavior.cs" />
<Compile Include="SamplePages\EnumValuesExtension\EnumValuesExtensionPage.xaml.cs">
<DependentUpon>EnumValuesExtensionPage.xaml</DependentUpon>
</Compile>
<Compile Include="SamplePages\TilesBrush\TilesBrushPage.xaml.cs">
<DependentUpon>TilesBrushPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -617,6 +621,8 @@
<Content Include="SamplePages\Triggers\UserHandPreferenceStateTrigger.bind" />
<Content Include="SamplePages\Triggers\UserInteractionModeStateTrigger.bind" />
<Content Include="SamplePages\StaggeredLayout\StaggeredLayout.bind" />
<Content Include="SamplePages\EnumValuesExtension\EnumValuesExtensionXaml.bind" />
<Content Include="SamplePages\EnumValuesExtension\EnumValuesExtensionCode.bind" />
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
Expand Down Expand Up @@ -995,6 +1001,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="SamplePages\EnumValuesExtension\EnumValuesExtensionPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="SamplePages\TilesBrush\TilesBrushPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Let's declare a sample enum
public enum Animal
{
Cat,
Dog,
Bunny,
Parrot,
Squirrel
}

// We can use a converter to get other values from our enum
public sealed class AnimalToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (Animal)value switch
{
Animal.Cat => Colors.Coral,
Animal.Dog => Colors.Gray,
Animal.Bunny => Colors.Green,
Animal.Parrot => Colors.YellowGreen,
Animal.Squirrel => Colors.SaddleBrown,
_ => throw new ArgumentException("Invalid value", nameof(value))
};
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.EnumValuesExtensionPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters"
mc:Ignorable="d">
<Page.Resources>
<enums:Animal x:Key="MyAnimal">Cat</enums:Animal>
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/>
</Page.Resources>
<Grid>
<StackPanel Spacing="8">
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"/>
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"/>
<Rectangle Height="16"
Width="16">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Windows.UI;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Microsoft.Toolkit.Uwp.SampleApp.Enums;

namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
/// <summary>
/// A page that shows how to use the <see cref="EnumValuesExtension"/> type.
/// </summary>
public sealed partial class EnumValuesExtensionPage : IXamlRenderListener
{
/// <summary>
/// Initializes a new instance of the <see cref="EnumValuesExtensionPage"/> class.
/// </summary>
public EnumValuesExtensionPage()
{
InitializeComponent();
}

public void OnXamlRendered(FrameworkElement control)
{
}
}
}

#pragma warning disable SA1403 // File may only contain a single namespace
namespace Microsoft.Toolkit.Uwp.SampleApp.Enums
{
public enum Animal
{
Cat,
Dog,
Bunny,
Parrot,
Squirrel
}
}

namespace Microsoft.Toolkit.Uwp.SampleApp.Converters
{
public sealed class AnimalToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (Animal)value switch
{
Animal.Cat => Colors.Coral,
Animal.Dog => Colors.Gray,
Animal.Bunny => Colors.Green,
Animal.Parrot => Colors.YellowGreen,
Animal.Squirrel => Colors.SaddleBrown,
_ => throw new ArgumentException("Invalid value", nameof(value))
};
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}

#pragma warning restore SA1403
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages.EnumValuesExtensionPage"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters"
mc:Ignorable="d">
<Page.Resources>
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/>
</Page.Resources>
<Grid>
<StackPanel Spacing="8"
HorizontalAlignment="Center"
VerticalAlignment="Center">

<!--Simple selector using the default template. Each item will
simply be displayed as the corresponding enum value name.-->
<TextBlock Text="Select an animal:"/>
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
SelectedIndex="0"/>

<!--We can also use a custom template and some converters.-->
<TextBlock Margin="0,12,0,0"
Text="Pick another animal:"/>
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid Width="80">
<TextBlock Text="{Binding}"/>
<Rectangle Height="16"
Width="16"
HorizontalAlignment="Right">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Grid>
</Page>
26 changes: 17 additions & 9 deletions Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -1266,15 +1266,23 @@
"XamlCodeFile": "OnDeviceXaml.bind",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/OnDeviceMarkup.md"
},
{
"Name": "IconExtensions",
"Type": "IconExtensionsPage",
"About": "Markup extensions to easily create various types of icons.",
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension",
"XamlCodeFile": "IconExtensionsXaml.bind",
"Icon": "/Assets/Helpers.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/IconExtensions.md"
}
{
"Name": "IconExtensions",
"Type": "IconExtensionsPage",
"About": "Markup extensions to easily create various types of icons.",
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension",
"XamlCodeFile": "IconExtensionsXaml.bind",
"Icon": "/Assets/Helpers.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/IconExtensions.md"
},
{
"Name": "EnumValuesExtension",
"Type": "EnumValuesExtensionPage",
"About": "The EnumValuesExtension markup extension allows you to easily retrieve a collection of all values from an enum type.",
"Icon": "/Assets/Helpers.png",
"XamlCodeFile": "EnumValuesExtensionXaml.bind",
"CodeFile": "EnumValuesExtensionCode.bind"
}
]
},
{
Expand Down
24 changes: 24 additions & 0 deletions Microsoft.Toolkit.Uwp.UI/Extensions/Markup/EnumValuesExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Windows.UI.Xaml.Markup;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
{
/// <summary>
/// A markup extension that returns a collection of values of a specific <see langword="enum"/>
/// </summary>
[MarkupExtensionReturnType(ReturnType = typeof(Array))]
public sealed class EnumValuesExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="System.Type"/> of the target <see langword="enum"/>
/// </summary>
public Type Type { get; set; }

/// <inheritdoc/>
protected override object ProvideValue() => Enum.GetValues(Type);
}
}
48 changes: 48 additions & 0 deletions UnitTests/UnitTests.UWP/Extensions/Test_EnumValuesExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace UnitTests.Extensions
{
[TestClass]
public class Test_EnumValuesExtension
{
[TestCategory("EnumValuesExtension")]
[UITestMethod]
public void Test_EnumValuesExtension_MarkupExtension()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:local=""using:UnitTests.Extensions"">
<ListView x:Name=""Check"" ItemsSource=""{ex:EnumValues Type=local:Animal}""/>
</Page>") as FrameworkElement;

var list = treeroot.FindChildByName("Check") as ListView;

Assert.IsNotNull(list, "Could not find listview control in tree.");

Animal[] items = list.ItemsSource as Animal[];

Assert.IsNotNull(items, "The items were not created correctly");

CollectionAssert.AreEqual(items, Enum.GetValues(typeof(Animal)));
}
}

public enum Animal
{
Cat,
Dog,
Bunny
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.UWP/Properties/UnitTestApp.rd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.FontIconSourceExtension" Dynamic="Required All" Serialize="Required All"/>
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.SymbolIconExtension" Dynamic="Required All" Serialize="Required All"/>
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.SymbolIconSourceExtension" Dynamic="Required All" Serialize="Required All"/>
<Type Name="Microsoft.Toolkit.Uwp.UI.Extensions.EnumValuesExtension" Dynamic="Required All" Serialize="Required All"/>

</Application>
</Directives>
4 changes: 4 additions & 0 deletions UnitTests/UnitTests.UWP/UnitTestApp.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
RequestedTheme="Light" >
<Application.Resources>

<extensions:EnumValuesExtension x:Key="DummyExtension"/>

<unitTestExtensions:Animal x:Key="Animal">Cat</unitTestExtensions:Animal>

<!-- Workarounds for .NET Native issue in unit tests -->
<CommandBar x:Key="DummyCommandBar">
<AppBarButton Icon="{extensions:FontIcon Glyph=&#xE102;}"/>
Expand Down
1 change: 1 addition & 0 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<Compile Include="Extensions\Test_BitmapIconExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_FontIconSourceExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_FontIconExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_EnumValuesExtension.cs" />
<Compile Include="Extensions\Test_NullableBoolMarkupExtension.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Helpers\TestCollectionCapableDeepLinkParser.cs" />
Expand Down