Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Replace Exception approach by extension used by XF #1028

Merged
merged 3 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Android.Graphics;
using Android.Widget;
using Xamarin.CommunityToolkit.Effects;
using Xamarin.CommunityToolkit.Extensions;
using Xamarin.Forms.Platform.Android;
using Effects = Xamarin.CommunityToolkit.Android.Effects;

Expand Down Expand Up @@ -33,8 +34,10 @@ protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)

void ApplyTintColor()
{
if (Control == null || Element == null)
if (!Control.IsAlive() || Element == null)
{
return;
}

var color = IconTintColorEffect.GetTintColor(Element);

Expand All @@ -51,21 +54,21 @@ void ApplyTintColor()

void ClearTintColor()
{
try
// Because of a XF bug: https://github.com/xamarin/Xamarin.Forms/issues/13889
if (!Control.IsAlive())
{
switch (Control)
{
case ImageView image:
image.ClearColorFilter();
break;
case Button button:
foreach (var drawable in button.GetCompoundDrawables())
drawable?.ClearColorFilter();
break;
}
return;
AndreiMisiukevich marked this conversation as resolved.
Show resolved Hide resolved
}
catch (ObjectDisposedException) {
// We ignore ObjectDisposedException as a workaround of XF issue https://github.com/xamarin/Xamarin.Forms/issues/13889

switch (Control)
{
case ImageView image:
image.ClearColorFilter();
break;
case Button button:
foreach (var drawable in button.GetCompoundDrawables())
drawable?.ClearColorFilter();
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Xamarin.CommunityToolkit.Extensions
{
internal static class JavaObjectExtensions
{
public static bool IsDisposed(this Java.Lang.Object obj)
=> obj.Handle == IntPtr.Zero;

public static bool IsAlive(this Java.Lang.Object obj)
=> obj != null && !obj.IsDisposed();

public static bool IsDisposed(this global::Android.Runtime.IJavaObject obj)
=> obj.Handle == IntPtr.Zero;

public static bool IsAlive(this global::Android.Runtime.IJavaObject obj)
=> obj != null && !obj.IsDisposed();
}
}