diff --git a/MvvmCross.Plugins/Color/MvxColorValueConverter.cs b/MvvmCross.Plugins/Color/MvxColorValueConverter.cs index 620db8b7e5..6904716ff2 100644 --- a/MvvmCross.Plugins/Color/MvxColorValueConverter.cs +++ b/MvvmCross.Plugins/Color/MvxColorValueConverter.cs @@ -1,44 +1,35 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MS-PL license. // See the LICENSE file in the project root for more information. - -using System; +#nullable enable using System.Globalization; using MvvmCross.Converters; using MvvmCross.UI; -namespace MvvmCross.Plugin.Color -{ -#nullable enable - public abstract class MvxColorValueConverter : MvxValueConverter - { - private readonly IMvxNativeColor? _nativeColor; +namespace MvvmCross.Plugin.Color; - protected MvxColorValueConverter() - { - Mvx.IoCProvider?.TryResolve(out _nativeColor); - } +public abstract class MvxColorValueConverter : MvxValueConverter +{ + private readonly Lazy _nativeColor = new(() => Mvx.IoCProvider?.Resolve()); - protected abstract System.Drawing.Color Convert(object value, object? parameter, CultureInfo? culture); + protected abstract System.Drawing.Color Convert(object value, object? parameter, CultureInfo? culture); - public sealed override object Convert(object value, Type? targetType, object? parameter, - CultureInfo? culture) - { - return _nativeColor?.ToNative(Convert(value, parameter, culture)) ?? MvxBindingConstant.UnsetValue; - } + public sealed override object Convert(object value, Type? targetType, object? parameter, + CultureInfo? culture) + { + return _nativeColor.Value?.ToNative(Convert(value, parameter, culture)) ?? MvxBindingConstant.UnsetValue; } +} - public abstract class MvxColorValueConverter : MvxColorValueConverter +public abstract class MvxColorValueConverter : MvxColorValueConverter +{ + protected sealed override System.Drawing.Color Convert(object value, object? parameter, CultureInfo? culture) { - protected sealed override System.Drawing.Color Convert(object value, object? parameter, CultureInfo? culture) - { - if (value is T t) - return Convert(t, parameter, culture); + if (value is T t) + return Convert(t, parameter, culture); - return default; - } - - protected abstract System.Drawing.Color Convert(T value, object? parameter, CultureInfo? culture); + return default; } -#nullable restore + + protected abstract System.Drawing.Color Convert(T value, object? parameter, CultureInfo? culture); } diff --git a/MvvmCross/Binding/Binders/MvxNamedInstanceRegistryFiller.cs b/MvvmCross/Binding/Binders/MvxNamedInstanceRegistryFiller.cs index 01d5d96b63..f851bf6e55 100644 --- a/MvvmCross/Binding/Binders/MvxNamedInstanceRegistryFiller.cs +++ b/MvvmCross/Binding/Binders/MvxNamedInstanceRegistryFiller.cs @@ -5,6 +5,7 @@ using System; using System.Linq; using System.Reflection; +using Microsoft.Extensions.Logging; using MvvmCross.Base; using MvvmCross.IoC; @@ -88,9 +89,10 @@ where type.IsConventional() MvxBindingLog.Trace("Registering value converter {0}:{1}", pair.Name, pair.Type.Name); registry.AddOrOverwrite(pair.Name, converter); } - catch (Exception) + catch (Exception ex) { - // ignore this + MvxBindingLog.Instance?.LogError(ex, "Failed to register {Name} from {Type}", pair.Name, + pair.Type.Name); } } } diff --git a/Projects/Playground/Playground.Core/Converters/TextToColorValueConverter.cs b/Projects/Playground/Playground.Core/Converters/TextToColorValueConverter.cs new file mode 100644 index 0000000000..06a53c2605 --- /dev/null +++ b/Projects/Playground/Playground.Core/Converters/TextToColorValueConverter.cs @@ -0,0 +1,24 @@ +using System.Drawing; +using System.Globalization; +using MvvmCross.Plugin.Color; + +namespace Playground.Core.Converters; + +// Sample converter to show issue found in GH issue #4803 +public sealed class TextToColorValueConverter : MvxColorValueConverter +{ + protected override Color Convert(object value, object parameter, CultureInfo culture) + { + if (value is not string stringValue) + return Color.Magenta; + + return stringValue switch + { + "I am green!" => Color.Green, + "I am yellow!" => Color.Yellow, + "I am brown!" => Color.Brown, + "I am orange!" => Color.Orange, + _ => Color.Black + }; + } +} diff --git a/Projects/Playground/Playground.Core/Playground.Core.csproj b/Projects/Playground/Playground.Core/Playground.Core.csproj index d9a513c96b..b1d30066a0 100644 --- a/Projects/Playground/Playground.Core/Playground.Core.csproj +++ b/Projects/Playground/Playground.Core/Playground.Core.csproj @@ -15,6 +15,7 @@ + diff --git a/Projects/Playground/Playground.Droid/Resources/layout/activity_converters.xml b/Projects/Playground/Playground.Droid/Resources/layout/activity_converters.xml index bb54eaef07..2febdd26a7 100644 --- a/Projects/Playground/Playground.Droid/Resources/layout/activity_converters.xml +++ b/Projects/Playground/Playground.Droid/Resources/layout/activity_converters.xml @@ -36,7 +36,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" - local:MvxBind="Text ColorText; TextColor NativeColor(TextColor)"/> + local:MvxBind="Text ColorText; TextColor TextToColor(ColorText)"/>