Skip to content

Commit eaeabb4

Browse files
committed
Added try catch
1 parent eeeee7c commit eaeabb4

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

MaterialDesignThemes.Wpf/PaletteHelper.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,25 @@ private static bool ReplaceEntry(object entryName, object newValue, ResourceDict
134134
{
135135
if (animate) //Fade animation is enabled
136136
{
137-
ColorAnimation animation = new ColorAnimation()
137+
try
138138
{
139-
From = (Color)parentDictionary[entryName],//The old color
140-
To = (Color)newValue, //The new color
141-
Duration = new Duration(new TimeSpan(0,0,0,0,DURATION_MS))
142-
};
143-
parentDictionary[entryName] = parentDictionary[entryName] == null ? new SolidColorBrush() : parentDictionary[entryName];
144-
(parentDictionary[entryName] as SolidColorBrush).BeginAnimation(SolidColorBrush.ColorProperty, animation); //Begin the animation
139+
ColorAnimation animation = new ColorAnimation()
140+
{
141+
From = (Color)parentDictionary[entryName],//The old color
142+
To = (Color)newValue, //The new color
143+
Duration = new Duration(new TimeSpan(0,0,0,0,DURATION_MS)) //Set the duration
144+
};
145+
parentDictionary[entryName] = parentDictionary[entryName] == null ? new SolidColorBrush() : parentDictionary[entryName]; //Set the resource as a new SolidColorBrush if null.
146+
(parentDictionary[entryName] as SolidColorBrush).BeginAnimation(SolidColorBrush.ColorProperty, animation); //Begin the animation
147+
}
148+
catch
149+
{
150+
System.Diagnostics.Debug.WriteLine(@"The argument :" + nameof(newValue) + " is not a brush. It is recommended to call the method like this ReplaceEntry(""SomeColor"", somethingnotabrush, null, false) to improve performance. Setting value without animation");
151+
goto setval; //Set the value normally if type is incorrect
152+
}
145153
}
146154
else
147-
parentDictionary[entryName] = newValue;
155+
setval: parentDictionary[entryName] = newValue; //Set value normally
148156
return true;
149157
}
150158
foreach (var dictionary in parentDictionary.MergedDictionaries)

0 commit comments

Comments
 (0)