From 40589b5da7df2887b541f731e99e08e03850a988 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:03:42 +0100 Subject: [PATCH 1/3] Add additional generic type definition to allow for the ComparingValue to be defined --- .../Converters/CompareConverter.shared.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CommunityToolkit.Maui/Converters/CompareConverter.shared.cs b/src/CommunityToolkit.Maui/Converters/CompareConverter.shared.cs index d15d7b32a..a8b54e7f2 100644 --- a/src/CommunityToolkit.Maui/Converters/CompareConverter.shared.cs +++ b/src/CommunityToolkit.Maui/Converters/CompareConverter.shared.cs @@ -7,14 +7,14 @@ namespace CommunityToolkit.Maui.Converters; /// /// Converts an object that implements IComparable to an object or a boolean based on a comparison. /// -public sealed class CompareConverter : CompareConverter +public sealed class CompareConverter : CompareConverter { } /// /// Converts an object that implements IComparable to an object or a boolean based on a comparison. /// -public abstract class CompareConverter : BaseConverterOneWay +public abstract class CompareConverter : BaseConverterOneWay where TValue : IComparable { /// public override object DefaultConvertReturnValue { get; set; } = new(); @@ -59,7 +59,7 @@ public enum OperatorType /// /// The comparing value. /// - public IComparable? ComparingValue { get; set; } + public TValue? ComparingValue { get; set; } /// /// The comparison operator. @@ -69,12 +69,12 @@ public enum OperatorType /// /// The object that corresponds to True value. /// - public TObject? TrueObject { get; set; } + public TReturnObject? TrueObject { get; set; } /// /// The object that corresponds to False value. /// - public TObject? FalseObject { get; set; } + public TReturnObject? FalseObject { get; set; } /// /// Converts an object that implements IComparable to a specified object or a boolean based on a comparison result. @@ -83,7 +83,7 @@ public enum OperatorType /// The culture to use in the converter. This is not implemented. /// The object assigned to if (value ) equals True and is not null, if is null it returns true, otherwise the value assigned to , if no value is assigned then it returns false. [MemberNotNull(nameof(ComparingValue))] - public override object ConvertFrom(IComparable value, CultureInfo? culture = null) + public override object ConvertFrom(TValue value, CultureInfo? culture = null) { ArgumentNullException.ThrowIfNull(value); ArgumentNullException.ThrowIfNull(ComparingValue); From 15c6c1530eca7334eb240dd7bd92ed7001d631d4 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:04:02 +0100 Subject: [PATCH 2/3] Update the sample application to make use of the ability to simplify XAML usage --- .../Converters/CompareDoubleToColorConverter.cs | 11 +++++++++++ .../Pages/Converters/CompareConverterPage.xaml | 14 +++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 samples/CommunityToolkit.Maui.Sample/Converters/CompareDoubleToColorConverter.cs diff --git a/samples/CommunityToolkit.Maui.Sample/Converters/CompareDoubleToColorConverter.cs b/samples/CommunityToolkit.Maui.Sample/Converters/CompareDoubleToColorConverter.cs new file mode 100644 index 000000000..d79c4275a --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Converters/CompareDoubleToColorConverter.cs @@ -0,0 +1,11 @@ +using CommunityToolkit.Maui.Converters; + +namespace CommunityToolkit.Maui.Sample.Converters; + +/// +/// Compares a double value using the +/// and returns a based on the comparison. +/// +public sealed class CompareDoubleToColorConverter : CompareConverter +{ +} \ No newline at end of file diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Converters/CompareConverterPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Converters/CompareConverterPage.xaml index a114554db..9f359c7c0 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Converters/CompareConverterPage.xaml +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Converters/CompareConverterPage.xaml @@ -2,6 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.CompareConverterPage" xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Converters" + xmlns:converters="clr-namespace:CommunityToolkit.Maui.Sample.Converters" xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages" x:TypeArguments="vm:CompareConverterViewModel" @@ -12,6 +13,13 @@ 0.5 LightGreen PaleVioletRed + + @@ -22,14 +30,14 @@ TextColor="{StaticResource NormalLabelTextColor}"/>