CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.28k stars 404 forks source link

[BUG] [MacCatalyst] IconTintColorBehavior Throwing Null Exception #2313

Open Bensley96 opened 2 weeks ago

Bensley96 commented 2 weeks ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

If you change the image source of an Image on MacCatalyst (and I'm assuming iOS as they share the same behaviour) a null exception is thrown due to what I believe is a race condition.

In the best-case scenario, the image just disappears. In the worst-case scenario, if the exception happens somewhere unhandled, the app will crash.

Expected Behavior

That when you adjust the image of a button, the image updates and the tint is applied without throwing the null exception and potentially crashing the app.

Steps To Reproduce

  1. Open and run the solution
  2. Click Button 1; Observe it disappears and the console shows a null reference
  3. Click Button 2; Observe the image source updates correctly

Link to public reproduction project repository

https://github.com/Bensley96/CommunityToolKit-Maui-Bugs

Environment

Anything else?

You'll notice under the MacCatylst folder i have a SafeIconTintColorBehavior.cs script that has a null check for 'button.currentImage'. The second button uses this script and updates the image correctly.

Curiously in the demo, if you use Shell the image disappears whereas without Shell the exception is catchable.

I have a pull request ready to submit for this bug once this is approved that updates line 101 on 'IconTintColorBehavior.macios.cs' to:

if (button.ImageView.Image is null || button.CurrentImage is null) { return; }

This is the same fix I made to the SafeIconTintcolorBehavior in my project. If this is ok I'll submit the pull request

Thanks,

Bensley

brminnick commented 2 weeks ago

Can you reproduce this using .NET 8.0? We don't yet support .NET 9.0.

Bensley96 commented 2 weeks ago

Was able to reproduce in .NET Version: 8.0.10

bijington commented 2 weeks ago

Yes please do submit the pull request