dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.14k stars 1.74k forks source link

NRE when setting images on buttons with a null ImageSource on iOS in 8.0.92 #25409

Open LeadAssimilator opened 3 days ago

LeadAssimilator commented 3 days ago

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

  1. Create a new maui app
  2. Update maui controls to version 8.0.91
  3. 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
  4. Run on an iOS Simulator and click the button
  5. Observe button image changes
  6. Update maui controls to version 8.0.92
  7. Run on an iOS Simulator and click the button
  8. 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

jaosnz-rep commented 2 days ago

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: Image 8.0.92: Image