Skip to content

Commit

Permalink
Fixing crash in IconTintColorBehavior (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulNiemiec authored May 27, 2024
1 parent e5b6776 commit 61f2415
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ namespace CommunityToolkit.Maui.Behaviors;

public partial class IconTintColorBehavior
{
AView? nativeView;

/// <inheritdoc/>
protected override void OnAttachedTo(View bindable, AView platformView)
{
base.OnAttachedTo(bindable, platformView);
nativeView = platformView;

ApplyTintColor(bindable, platformView);
ApplyTintColor();

bindable.PropertyChanged += OnElementPropertyChanged;
this.PropertyChanged += (s, e) =>
PropertyChanged += OnTintedImagePropertyChanged;
}

void OnTintedImagePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == TintColorProperty.PropertyName)
{
if (e.PropertyName == TintColorProperty.PropertyName)
{
ApplyTintColor(bindable, platformView);
}
};
ApplyTintColor();
}
}

/// <inheritdoc/>
Expand All @@ -35,13 +40,19 @@ protected override void OnDetachedFrom(View bindable, AView platformView)

ClearTintColor(bindable, platformView);
bindable.PropertyChanged -= OnElementPropertyChanged;
PropertyChanged -= OnTintedImagePropertyChanged;
}

void ApplyTintColor(View element, AView control)
void ApplyTintColor()
{
var color = TintColor;

switch (control)
if (nativeView is null)
{
return;
}

switch (nativeView)
{
case ImageView image:
SetImageViewTintColor(image, color);
Expand Down Expand Up @@ -100,7 +111,7 @@ void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs args)
return;
}

ApplyTintColor(bindable, platformView);
ApplyTintColor();
}

void ClearTintColor(View element, AView control)
Expand Down

0 comments on commit 61f2415

Please sign in to comment.