Skip to content
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

BitmapIcon markup extension #3130

Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3057376
Added FontIcon markup extension
Sergio0694 Jan 25, 2020
728f1ce
Removed unnecessary using directives
Sergio0694 Jan 25, 2020
800efba
Renamed the FontIcon markup extension
Sergio0694 Jan 25, 2020
c2f00be
Added FontIcon tests
Sergio0694 Jan 25, 2020
4493ffc
Fixed a typo in the test XAML
Sergio0694 Jan 26, 2020
6418ba5
Merge branch 'master' into feature/font-icon-extension
michael-hawker Feb 11, 2020
e8d4309
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 Feb 12, 2020
6638416
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 Feb 12, 2020
452a276
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 Feb 12, 2020
5577118
Added new FontIcon properties
Sergio0694 Feb 12, 2020
9018d88
Merge branch 'feature/font-icon-extension' of https://github.com/Serg…
Sergio0694 Feb 12, 2020
870306c
Added more tests for the new properties
Sergio0694 Feb 12, 2020
c6d6023
Added FontIconSourceExtension
Sergio0694 Feb 12, 2020
e90b0eb
Added BitmapIconExtension
Sergio0694 Feb 12, 2020
8dda65e
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Feb 12, 2020
cea30df
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Feb 12, 2020
1b34996
Merge branch 'master' into feature/font-icon-extension
Sergio0694 Mar 15, 2020
23dcec0
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Mar 15, 2020
63c2b6e
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Mar 15, 2020
96b22d4
Merge branch 'master' into feature/font-icon-extension
Sergio0694 Mar 24, 2020
7d6f9ec
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Mar 24, 2020
91947c1
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Mar 24, 2020
0d8f538
Added tests for the FontIconSource extension
Sergio0694 Mar 24, 2020
1af9349
Fixed namespace for BitmapIconExtension type
Sergio0694 Mar 24, 2020
bdcfd9a
Added tests for BitmapIconExtension type
Sergio0694 Mar 24, 2020
88844c1
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Mar 24, 2020
d0421d8
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Mar 24, 2020
1d9ad41
Merge branch 'master' into feature/font-icon-extension
Sergio0694 Mar 24, 2020
ae5f94b
Merge pull request #14 from Sergio0694/feature/font-icon-source-exten…
Sergio0694 Mar 24, 2020
76cfed2
Merge branch 'feature/font-icon-extension' into feature/bitmap-icon-e…
Sergio0694 Mar 24, 2020
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
39 changes: 39 additions & 0 deletions Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
{
/// <summary>
/// Custom <see cref="MarkupExtension"/> which can provide <see cref="BitmapIcon"/> values.
/// </summary>
[Bindable]
[MarkupExtensionReturnType(ReturnType = typeof(BitmapIcon))]
public sealed class BitmapIconExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="Uri"/> representing the image to display.
/// </summary>
public Uri Source { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to display the icon as monochrome.
/// </summary>
public bool ShowAsMonochrome { get; set; }

/// <inheritdoc/>
protected override object ProvideValue()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected override object ProvideValue() [](start = 8, length = 40)

It can be a body-less method:

protected override object ProvideValue() => new BitmapIcon
{
    ShowAsMonochrome = ShowAsMonochrome,
    UriSource = Source
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a fan of how the body expression looks in small methods returning an object like this, as at a glance it's easy to confuse the brackets under the method with the actual brackets for the method body, when in fact they belong to that constructor instead.
I'd prefer to leave this as is if that's alright, but let me know what you think! 😊

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vgromfeld do you have any pros/cons around the styling of this one way or the other, I know this is a newer feature of C# compared to when the Toolkit started, so I don't think we have any explicit styling guidelines setup for this scenario. Honestly though, I'm fine with either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body-less generally allows us to remove all the unneeded characters and write more compact code. It might be less natural first but really helps to read the code faster once you get use to it 😀.
VS2019 code style is recommending this approach. It definitively don't worth a full restyling of all the toolkit code but should be more and more considered when writing new code.

{
return new BitmapIcon
{
ShowAsMonochrome = ShowAsMonochrome,
UriSource = Source
};
}
}
}
85 changes: 85 additions & 0 deletions UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 System.Linq;
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_BitmapIconExtensionMarkupExtension
{
[TestCategory("BitmapIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_BitmapIconExtension_MarkupExtension_ProvideImage()
{
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"">
<Button x:Name=""RootButton"">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Icon=""{ex:BitmapIcon Source=/Assets/StoreLogo.png}"" />
</MenuFlyout>
</Button.Flyout>
</Button>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("RootButton") as Button;

Assert.IsNotNull(button, $"Could not find the {nameof(Button)} control in tree.");

var item = ((MenuFlyout)button.Flyout)?.Items?.FirstOrDefault() as MenuFlyoutItem;

Assert.IsNotNull(button, $"Could not find the target {nameof(MenuFlyoutItem)} control.");

var icon = item.Icon as BitmapIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(BitmapIcon)} element in button.");

Assert.AreEqual(icon.UriSource, new Uri("ms-resource:///Files/Assets/StoreLogo.png"), "Expected ms-resource:///Files/Assets/StoreLogo.png uri.");
Assert.AreEqual(icon.ShowAsMonochrome, false, "Expected icon not to be monochrome");
}

[TestCategory("BitmapIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_BitmapIconExtension_MarkupExtension_ProvideImageAndMonochrome()
{
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"">
<Button x:Name=""RootButton"">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Icon=""{ex:BitmapIcon Source=/Assets/StoreLogo.png, ShowAsMonochrome=True}"" />
</MenuFlyout>
</Button.Flyout>
</Button>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("RootButton") as Button;

Assert.IsNotNull(button, $"Could not find the {nameof(Button)} control in tree.");

var item = ((MenuFlyout)button.Flyout)?.Items?.FirstOrDefault() as MenuFlyoutItem;

Assert.IsNotNull(button, $"Could not find the target {nameof(MenuFlyoutItem)} control.");

var icon = item.Icon as BitmapIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(BitmapIcon)} element in button.");

Assert.AreEqual(icon.UriSource, new Uri("ms-resource:///Files/Assets/StoreLogo.png"), "Expected ms-resource:///Files/Assets/StoreLogo.png uri.");
Assert.AreEqual(icon.ShowAsMonochrome, true, "Expected icon to be monochrome");
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Diagnostics\Test_Guard.Array.cs" />
<Compile Include="Diagnostics\Test_Guard.cs" />
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
<Compile Include="Extensions\Test_BitmapIconExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_TypeExtensions.cs" />
<Compile Include="Extensions\Test_ValueTypeExtensions.cs" />
<Compile Include="Extensions\Test_ArrayExtensions.cs" />
Expand Down