Skip to content

Commit

Permalink
Merge branch 'master' into winui
Browse files Browse the repository at this point in the history
  • Loading branch information
azchohfi committed Jul 15, 2020
2 parents 2c7b325 + 8756f29 commit 536d511
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 12 deletions.
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 Microsoft.Toolkit.Uwp.SampleApp.Enums;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;

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 @@ -1243,15 +1243,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
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
<comment>Narrator Resource for BladeView expanded status</comment>
</data>
<data name="WindowsCommunityToolkit_GridSplitter_AutomationName" xml:space="preserve">
<value>GridSpliter</value>
<value>GridSplitter</value>
<comment>Narrator Resource for GridSplitter control</comment>
</data>
<data name="WindowsCommunityToolkit_InAppNotification_Events_NewNotificationMessage" xml:space="preserve">
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.Uwp.UI.Media/Effects/BlendEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Toolkit.Uwp.UI.Media.Pipelines;
using Windows.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Markup;

namespace Microsoft.Toolkit.Uwp.UI.Media.Effects
{
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 Microsoft.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="global::System.Type"/> of the target <see langword="enum"/>
/// </summary>
public Type Type { get; set; }

/// <inheritdoc/>
protected override object ProvideValue() => Enum.GetValues(Type);
}
}
5 changes: 5 additions & 0 deletions UnitTests/UnitTests.UWP/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
</Button>
</ResourceDictionary>
</Application.Resources>

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

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

</Application>
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>
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The Windows Community Toolkit is a collection of helper functions, custom contro
## Build Status
| Target | Branch | Status | Recommended package version |
| ------ | ------ | ------ | ------ |
| Production | rel/6.0.0 | [![Build Status](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_apis/build/status/Toolkit-CI?branchName=rel/6.0.0)](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_build/latest?definitionId=10&branchName=rel/6.0.0) | [![NuGet](https://img.shields.io/nuget/v/Microsoft.Toolkit.Uwp.svg)](https://www.nuget.org/profiles/Microsoft.Toolkit) |
| Production | rel/6.1.0 | [![Build Status](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_apis/build/status/Toolkit-CI?branchName=rel/6.1.0)](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_build/latest?definitionId=10&branchName=rel/6.1.0) | [![NuGet](https://img.shields.io/nuget/v/Microsoft.Toolkit.Uwp.svg)](https://www.nuget.org/profiles/Microsoft.Toolkit) |
| Pre-release beta testing | master | [![Build Status](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_apis/build/status/Toolkit-CI?branchName=master)](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_build/latest?definitionId=10) | [![MyGet](https://img.shields.io/dotnet.myget/uwpcommunitytoolkit/vpre/Microsoft.Toolkit.Uwp.svg)](https://dotnet.myget.org/gallery/uwpcommunitytoolkit) |

## Getting Started
Expand Down

0 comments on commit 536d511

Please sign in to comment.