Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine enum conversion logic in IsEqualStateTrigger #3678

Merged
20 changes: 14 additions & 6 deletions Microsoft.Toolkit.Uwp.UI/Triggers/IsEqualStateTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ public object To

internal static bool AreValuesEqual(object value1, object value2, bool convertType)
{
if (value1 == value2)
if (object.Equals(value1, value2))
{
return true;
}

if (value1 != null && value2 != null && convertType)
// If they are the same type but fail with Equals check, don't bother with conversion.
if (value1 != null && value2 != null && value1.GetType() != value2.GetType() && convertType)
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved
{
// Try the conversion in both ways:
return ConvertTypeEquals(value1, value2) || ConvertTypeEquals(value2, value1);
Expand All @@ -92,14 +93,21 @@ private static bool ConvertTypeEquals(object value1, object value2)

private static object ConvertToEnum(Type enumType, object value)
{
try
// value cannot be the same type of enum now
if (value is string str)
{
return Enum.IsDefined(enumType, value) ? Enum.ToObject(enumType, value) : null;
return Enum.TryParse(enumType, str, out var e) ? e : null;
}
catch

if (value is int || value is uint
|| value is byte || value is sbyte
|| value is long || value is ulong
|| value is short || value is ushort)
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved
{
return null;
return Enum.ToObject(enumType, value);
}

return null;
}
}
}