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
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
Open and run the solution
Click Button 1; Observe it disappears and the console shows a null reference
Click Button 2; Observe the image source updates correctly
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
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
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