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
21.87k stars 1.69k forks source link

[Shell][iOS] Error when setting Shell search icon in search handler #14442

Open kklose23 opened 1 year ago

kklose23 commented 1 year ago

Description

Null reference exception when opening app within IOS that has a Shell search with an icon set within its search handler

Steps to Reproduce

  1. Follow these steps https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/search?view=net-maui-7.0
  2. Create a constructor for the AnimalSearchHandler and set ClearIcon = "clear_icon.png"; within the constructor
  3. Run the application in an IOS simulator

Link to public reproduction project repository

N/A

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 16.2

Did you find any workaround?

Yes,

After setting ClearIcon = "clear_icon.png";, set ClearIcon.Parent = Application.Current.MainPage;

This is because source.FindMauiContext() in ShellPageRendererTracker.SetSearchBarIcon was returning null. So setting a parent element on the icon fixes it.

Relevant log output

at Microsoft.Maui.ImageSourceExtensions.GetPlatformImageAsync(IImageSource imageSource, IMauiContext mauiContext)
   at Microsoft.Maui.ImageSourceExtensions.LoadImage(IImageSource source, IMauiContext mauiContext, Action`1 finished)
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellPageRendererTracker.SetSearchBarIcon(UISearchBar searchBar, ImageSource source, UISearchBarIcon icon)
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellPageRendererTracker.AttachSearchController()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellPageRendererTracker.set_SearchHandler(SearchHandler value)
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellPageRendererTracker.UpdateShellToMyPage()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellPageRendererTracker.OnPageSet(Page oldPage, Page newPage)
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellPageRendererTracker.set_Page(Page value)
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
isax5 commented 1 year ago

I'm facing the same problem

PureWeen commented 7 months ago

Related https://github.com/dotnet/maui/issues/19504

Larhei commented 7 months ago

After debugging it. The Nullpointer is because the maui context is null.

ViewExtensions FindMauiContext is returning null in this line

devhls commented 5 months ago

Did somebody have a chance to make the workaround without the search handler removal?

kklose23 commented 5 months ago

@devhls I wrote a workaround in the "Did you find any workaround?" section

Zangl commented 2 months ago

@devhls Did the workaround work for you? I've tried and I still have this issue on iOS cc @kklose23

devhls commented 3 weeks ago

@Zangl Yes, I use this workaround with some details:

x:Name="MySearchHandler"

 public MyPage()
    {
        InitializeComponent();
        MySearchHandler.ClearIcon.Parent = this;
        MySearchHandler.QueryIcon.Parent = this;
    }

If I remember right The app works without exceptions, but I use default icons. I am not sure if this helps to use custom icons.