From f242cec2556a4cf9cd8c9b964aa3cd25fc01a0ec Mon Sep 17 00:00:00 2001 From: Kay-STL <155704370+Kay-STL@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:16:39 +1200 Subject: [PATCH] fix: Fix unhandled ObjectDisposedException in IconTintColorBehavior.ClearTintColor on Android (#2012) --- .../IconTintColor/IconTintColorBehavior.android.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs index 4e0fe93a6..5d3458fd9 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs @@ -31,7 +31,7 @@ protected override void OnDetachedFrom(View bindable, AndroidView platformView) { base.OnDetachedFrom(bindable, platformView); - ClearTintColor(bindable, platformView); + ClearTintColor(platformView); bindable.PropertyChanged -= OnElementPropertyChanged; PropertyChanged -= OnTintedImagePropertyChanged; @@ -62,7 +62,6 @@ static void ApplyTintColor(AndroidView? nativeView, Color? tintColor) throw new NotSupportedException($"{nameof(IconTintColorBehavior)} only currently supports Android.Widget.Button and {nameof(ImageView)}."); } - static void SetImageViewTintColor(ImageView image, Color? color) { if (color is null) @@ -103,9 +102,14 @@ static void SetWidgetButtonTintColor(AndroidWidgetButton button, Color? color) } } - static void ClearTintColor(View element, AndroidView control) + static void ClearTintColor(AndroidView? nativeView) { - switch (control) + if (nativeView is null) + { + return; + } + + switch (nativeView) { case ImageView image: image.ClearColorFilter(); @@ -114,7 +118,6 @@ static void ClearTintColor(View element, AndroidView control) case AndroidMaterialButton mButton: mButton.IconTint = null; break; - case AndroidWidgetButton button: foreach (var drawable in button.GetCompoundDrawables()) {