-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
michael-hawker
merged 13 commits into
CommunityToolkit:master
from
Sergio0694:feature/enum-values-markdown-extension
Jul 10, 2020
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ffe5042
Added EnumValuesExtension
Sergio0694 02e15e6
Added EnumValuesExtension unit test
Sergio0694 c54efb9
Merge branch 'master' into feature/enum-values-markdown-extension
Sergio0694 8fe1aea
Added EnumValuesExtension to .rd.xml file
Sergio0694 65ed50e
Merge branch 'master' into feature/enum-values-markdown-extension
Sergio0694 4b7eb2a
Fixed test for EnumValuesExtension
Sergio0694 6d85170
Added empty EnumValuesExtensionPage setup
Sergio0694 2279200
Added initial sample with direct enum display
Sergio0694 41065bc
Added second sample with a converter
Sergio0694 15cecd4
Removed empty ItemGroup from sample app .csproj
Sergio0694 bb895e8
Merge branch 'master' into feature/enum-values-markdown-extension
Sergio0694 9cb6079
Merge branch 'master' into feature/enum-values-markdown-extension
Sergio0694 389e9e1
Merge branch 'master' into feature/enum-values-markdown-extension
Sergio0694 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionCode.bind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionPage.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
70 changes: 70 additions & 0 deletions
70
...oft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionPage.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
47 changes: 47 additions & 0 deletions
47
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionXaml.bind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Microsoft.Toolkit.Uwp.UI/Extensions/Markup/EnumValuesExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
UnitTests/UnitTests.UWP/Extensions/Test_EnumValuesExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.