diff --git a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/UIContainerCell.cs b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/UIContainerCell.cs index 443b6af2aaaa..50bf114a860d 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/UIContainerCell.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/UIContainerCell.cs @@ -24,7 +24,7 @@ internal UIContainerCell(string cellId, View view, Shell shell, object context) if (_renderer == null) { - _renderer = (IPlatformViewHandler)view.ToHandler(shell.FindMauiContext()); + _renderer = (IPlatformViewHandler)view.ToHandler(view.FindMauiContext() ?? shell.FindMauiContext()); } ContentView.AddSubview(_renderer.PlatformView); diff --git a/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs b/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs index 89ea42051ba0..802e9405a0c0 100644 --- a/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs @@ -7,6 +7,8 @@ using Microsoft.Maui; using Microsoft.Maui.Controls; using Microsoft.Maui.Controls.Handlers; +using Microsoft.Maui.Controls.Handlers.Compatibility; +using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.Maui.Handlers; using Microsoft.Maui.Hosting; using Microsoft.Maui.Platform; @@ -34,6 +36,32 @@ void SetupBuilder() }); } +#if ANDROID || IOS || MACCATALYST + [Fact] + public async Task SearchHandlerRendersCorrectly() + { + SetupBuilder(); + + var shell = await CreateShellAsync(shell => + { + shell.Items.Add(new FlyoutItem() { Route = "FlyoutItem1", Items = { new ContentPage() }, Title = "Flyout Item" }); + shell.Items.Add(new FlyoutItem() { Route = "FlyoutItem2", Items = { new ContentPage() }, Title = "Flyout Item" }); + + Shell.SetSearchHandler(shell, new SearchHandler() { SearchBoxVisibility = SearchBoxVisibility.Expanded }); + }); + + await CreateHandlerAndAddToWindow(shell, async (handler) => + { + await OnLoadedAsync(shell.CurrentPage); + await OnNavigatedToAsync(shell.CurrentPage); + await Task.Delay(100); + await shell.GoToAsync("//FlyoutItem2"); + + // No crash using SearchHandler + }); + } +#endif + [Fact] public async Task FlyoutWithAsMultipleItemsRendersWithoutCrashing() {