NRE when setting images on buttons with a null ImageSource on iOS in 8.0.92
Description
8.0.92 made breaking changes and a bad assumption that if a UIButton.CurrentImage is non-null then Button.ImageSource must also be non-null. This isn't always correct and causes an NRE when not so. There should be no assumptions made as to the relation between UIButton.CurrentImage and Button.ImageSource, as anyone can set any image on a UIButton from anywhere (custom controls or random code) while Button.ImageSource is null and Button.ImageSource should be properly null checked in all cases it is used. This used to work fine in 8.0.91 and below even in XF and works fine on other platforms, just not iOS with the recent changes.
https://github.com/dotnet/maui/commit/029430146cd89f9d1941544e4ea451857527dbc2#diff-d903e6bb2cc5deb7be51e48b40d0207507f0dc84be56af77b6559644f64f95a8R84-R92
Steps to Reproduce
- Create a new maui app
- Update maui controls to version 8.0.91
- Add the following code to OnCounterClicked:
#if IOS
((Action)(async () =>
{
var imageSource = new FileImageSource() { File = "dotnet_bot.png" };
var imageResult = await imageSource.GetPlatformImageAsync(CounterBtn.Handler.MauiContext);
(CounterBtn.Handler.PlatformView as UIKit.UIButton).SetImage(imageResult.Value, UIKit.UIControlState.Normal);
}))();
#endif
- Run on an iOS Simulator and click the button
- Observe button image changes
- Update maui controls to version 8.0.92
- Run on an iOS Simulator and click the button
- Observe crash due to NRE
Link to public reproduction project repository
No response
Version with bug
8.0.92 SR9.2
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI, Yes, this used to work in Xamarin.Forms
Last version that worked well
8.0.91 SR9.1
Affected platforms
iOS
Affected platform versions
No response
Did you find any workaround?
No response
Relevant log output
No response
I can repro this issue at iOS platform on the latest 17.12.0 preview 3(8.0.92), but it works fine on 8.0.91.
8.0.91:
8.0.92: