Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix MvxColorValueConverter not working (#4807)
- Loading branch information
1 parent
af92938
commit 393e1e6
Showing
5 changed files
with
49 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Projects/Playground/Playground.Core/Converters/TextToColorValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters