Maui: [BUG] IconTintColorBehavior crash on android.
Is there an existing issue for this?
- I have searched the existing issues
Did you read the “Reporting a bug” section on Contributing file?
- I have read the “Reporting a bug” section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
IconTintColorBehavior crash while changing tint color.
System.ObjectDisposedException: ObjectDisposed_Generic ObjectDisposed_ObjectName_Name, AndroidX.AppCompat.Widget.AppCompatImageView at Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable ) at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String , IJavaPeerable , JniArgumentValue* ) at Android.Widget.ImageView.SetColorFilter(ColorFilter ) at CommunityToolkit.Maui.Behaviors.IconTintColorBehavior.<ApplyTintColor>g__SetImageViewTintColor|2_0(ImageView image, Color color)
Expected Behavior
IconTintColorBehavior does not crash.
Steps To Reproduce
Unfortunately, I have some nuget problems with adding package, but you should be able to reproduce it by changing tint color.
Link to public reproduction project repository
no link currently
Environment
- .NET MAUI CommunityToolkit:
- OS:
- .NET MAUI:
Anything else?
I found fix for this. Just remove PropertyChanged event handler in OnDetachedFrom method. In previous version handler is added, but not removed.
public partial class WorkaroundTintedImageBehavior { private View? _bindable; private AView? _platformView;
protected override void OnAttachedTo(View bindable, AView platformView)
{
_bindable = bindable;
_platformView = platformView;
bindable.PropertyChanged += OnElementPropertyChanged;
ApplyTintColor(platformView);
PropertyChanged += OnTintedImagePropertyChanged;
}
protected override void OnDetachedFrom(View bindable, AView platformView)
{
ClearTintColor(bindable, platformView);
bindable.PropertyChanged -= OnElementPropertyChanged;
PropertyChanged -= OnTintedImagePropertyChanged;
}
private void OnTintedImagePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == TintColorProperty.PropertyName && _bindable is not null && _platformView is not null)
{
ApplyTintColor(_platformView);
}
}
private void ApplyTintColor(AView control)
{
var color = TintColor;
switch (control)
{
case ImageView image:
SetImageViewTintColor(image, color);
break;
case AButton button:
SetButtonTintColor(button, color);
break;
default:
throw new NotSupportedException($"{nameof(WorkaroundTintedImageBehavior)} only currently supports Android.Widget.Button and {nameof(ImageView)}.");
}
static void SetImageViewTintColor(ImageView image, Color? color)
{
if (color is null)
{
image.ClearColorFilter();
return;
}
image.SetColorFilter(new PorterDuffColorFilter(color.ToPlatform(), PorterDuff.Mode.SrcIn ?? throw new InvalidOperationException("PorterDuff.Mode.SrcIn should not be null at runtime.")));
}
static void SetButtonTintColor(AButton button, Color? color)
{
var drawables = button.GetCompoundDrawables().Where(d => d is not null);
if (color is null)
{
foreach (var img in drawables)
{
img.ClearColorFilter();
}
color = Colors.Transparent;
}
foreach (var img in drawables)
{
img.SetTint(color.ToPlatform());
}
}
}
private void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs args)
{
if (args.PropertyName is not string propertyName
|| sender is not View bindable
|| bindable.Handler?.PlatformView is not AView platformView)
{
return;
}
if (!propertyName.Equals(Image.SourceProperty.PropertyName, StringComparison.Ordinal)
&& !propertyName.Equals(ImageButton.SourceProperty.PropertyName, StringComparison.Ordinal))
{
return;
}
ApplyTintColor(platformView);
}
private void ClearTintColor(View element, AView control)
{
element.PropertyChanged -= OnElementPropertyChanged;
switch (control)
{
case ImageView image:
image.ClearColorFilter();
break;
case AButton button:
foreach (var drawable in button.GetCompoundDrawables())
{
drawable?.ClearColorFilter();
}
break;
}
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (13 by maintainers)
@christian-strydom I will take a look during this weekend, after fix I wasn’t able to reproduce it
@PaulNiemiec thank you for the PR! The failure has nothing to do with your changes. It looks like a new version of MAUI is available and it’s broken our pipeline build. We will try and patch that up and then merge this in
@PaulNiemiec no problem at all! Thank you still for the offer/intention, it is greatly appreciated
@bijington I can try
@PaulNiemiec Thanks for giving more info. I have implemented your fix in my reproduction repo, and the app no longer crashes when following the steps I outlined. Great work!