Skip to content

Commit

Permalink
Fix MvxColorValueConverter not working (#4807)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheesebaron committed Feb 3, 2024
1 parent af92938 commit 393e1e6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
47 changes: 19 additions & 28 deletions 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<IMvxNativeColor>(out _nativeColor);
}
public abstract class MvxColorValueConverter : MvxValueConverter
{
private readonly Lazy<IMvxNativeColor?> _nativeColor = new(() => Mvx.IoCProvider?.Resolve<IMvxNativeColor>());

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<T> : MvxColorValueConverter
public abstract class MvxColorValueConverter<T> : 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);
}
6 changes: 4 additions & 2 deletions MvvmCross/Binding/Binders/MvxNamedInstanceRegistryFiller.cs
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using MvvmCross.Base;
using MvvmCross.IoC;

Expand Down Expand Up @@ -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);
}
}
}
Expand Down
@@ -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
};
}
}
1 change: 1 addition & 0 deletions Projects/Playground/Playground.Core/Playground.Core.csproj
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\MvvmCross.Plugins\Color\MvvmCross.Plugin.Color.csproj" />
<ProjectReference Include="..\..\..\MvvmCross.Plugins\JsonLocalization\MvvmCross.Plugin.JsonLocalization.csproj" />
<ProjectReference Include="..\..\..\MvvmCross.Plugins\Json\MvvmCross.Plugin.Json.csproj" />
<ProjectReference Include="..\..\..\MvvmCross.Plugins\Messenger\MvvmCross.Plugin.Messenger.csproj" />
Expand Down
Expand Up @@ -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)"/>

<Button
android:layout_width="match_parent"
Expand Down

0 comments on commit 393e1e6

Please sign in to comment.