From f4ab146361f66eae7e29658f83cab0df96d63f86 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Mon, 8 Jan 2024 10:16:02 -0800 Subject: [PATCH 1/3] Update Style.cs --- src/CommunityToolkit.Maui.Markup/Style.cs | 35 +++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/CommunityToolkit.Maui.Markup/Style.cs b/src/CommunityToolkit.Maui.Markup/Style.cs index 570a6a19..bebc92ff 100644 --- a/src/CommunityToolkit.Maui.Markup/Style.cs +++ b/src/CommunityToolkit.Maui.Markup/Style.cs @@ -7,15 +7,22 @@ public class Style where T : BindableObject { /// - /// FormsStyle + /// Converts to /// /// public static implicit operator Style(Style style) => style.MauiStyle; + /// + /// Converts to + /// + /// + /// + public static implicit operator Style(Style style) => new(style); + /// /// Initialize Style /// - /// "The to style + /// "The to mauiStyle /// "The value for the public Style(BindableProperty property, object value) : this((property, value)) { @@ -32,6 +39,20 @@ public Style(params (BindableProperty Property, object Value)[] setters) Add(setters); } + /// + /// Initialize Style + /// + /// + public Style(Style mauiStyle) + { + if (!mauiStyle.TargetType.IsAssignableTo(typeof(T))) + { + throw new ArgumentException($"Invalid type. The Type used in {nameof(mauiStyle)}.{nameof(mauiStyle.TargetType)} ({mauiStyle.TargetType.FullName}) must be assignable to the Type used in {nameof(Style)} ({typeof(T).FullName})", nameof(mauiStyle)); + } + + MauiStyle = new Style(typeof(T)); + } + /// /// Style(typeof(T)) /// @@ -62,7 +83,7 @@ public Style BasedOn(Style value) /// /// Add Setters /// - /// "The to style + /// "The to mauiStyle /// "The value for the /// Style with added setters public Style Add(BindableProperty property, object value) @@ -89,13 +110,17 @@ public Style Add(params (BindableProperty Property, object Value)[] setters) /// /// Adds the supplied and values in an . /// - /// "The to style + /// "The to mauiStyle /// "The light value for the /// "The dark value for the /// Style with added setters public Style AddAppThemeBinding(BindableProperty property, object light, object dark) { - MauiStyle.Setters.Add(property, new AppThemeBinding { Light = light, Dark = dark }); + MauiStyle.Setters.Add(property, new AppThemeBinding + { + Light = light, + Dark = dark + }); return this; } From 36a18576c04d6a0bdedf0cae30eefd497ffe5bfb Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Thu, 11 Jul 2024 11:19:20 -0700 Subject: [PATCH 2/3] Add Unit Tests --- .../StyleTests.cs | 57 ++++++++++++++++++- src/CommunityToolkit.Maui.Markup/Style.cs | 2 +- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/CommunityToolkit.Maui.Markup.UnitTests/StyleTests.cs b/src/CommunityToolkit.Maui.Markup.UnitTests/StyleTests.cs index cc84f0ce..e7f67b06 100644 --- a/src/CommunityToolkit.Maui.Markup.UnitTests/StyleTests.cs +++ b/src/CommunityToolkit.Maui.Markup.UnitTests/StyleTests.cs @@ -7,7 +7,36 @@ namespace CommunityToolkit.Maui.Markup.UnitTests; class StyleTests : BaseMarkupTestFixture { [Test] - public void ImplicitCast() + public void ImplicitCastToStyleT() + { + var formsStyle = new Style(typeof(Label)); + var style = (Style