Skip to content

Commit 607798b

Browse files
Merge pull request #3678 from huoyaoyuan/trigger-enum-parse
Refine enum conversion logic in IsEqualStateTrigger
2 parents d97513f + cc45cd9 commit 607798b

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Microsoft.Toolkit.Uwp.UI/Triggers/IsEqualStateTrigger.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ public object To
6161

6262
internal static bool AreValuesEqual(object value1, object value2, bool convertType)
6363
{
64-
if (value1 == value2)
64+
if (object.Equals(value1, value2))
6565
{
6666
return true;
6767
}
6868

69-
if (value1 != null && value2 != null && convertType)
69+
// If they are the same type but fail with Equals check, don't bother with conversion.
70+
if (value1 is not null && value2 is not null && convertType
71+
&& value1.GetType() != value2.GetType())
7072
{
7173
// Try the conversion in both ways:
7274
return ConvertTypeEquals(value1, value2) || ConvertTypeEquals(value2, value1);
@@ -92,14 +94,14 @@ private static bool ConvertTypeEquals(object value1, object value2)
9294

9395
private static object ConvertToEnum(Type enumType, object value)
9496
{
95-
try
97+
// value cannot be the same type of enum now
98+
return value switch
9699
{
97-
return Enum.IsDefined(enumType, value) ? Enum.ToObject(enumType, value) : null;
98-
}
99-
catch
100-
{
101-
return null;
102-
}
100+
string str => Enum.TryParse(enumType, str, out var e) ? e : null,
101+
int or uint or byte or sbyte or long or ulong or short or ushort
102+
=> Enum.ToObject(enumType, value),
103+
_ => null
104+
};
103105
}
104106
}
105107
}

0 commit comments

Comments
 (0)