From ff252dcdb69f0bee05e7bd5d4a052211add6339d Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Mon, 26 Apr 2021 20:07:29 -0500 Subject: [PATCH] - fix SO exception --- .../Platform/iOS/ControlsNavigationController.cs | 6 +++--- src/Core/src/Handlers/Page/PageHandler.iOS.cs | 9 ++++----- .../src/Platform/iOS/ContainerViewController.cs | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Controls/src/Core/Platform/iOS/ControlsNavigationController.cs b/src/Controls/src/Core/Platform/iOS/ControlsNavigationController.cs index 2e2b751c273d..53c8b45fcd65 100644 --- a/src/Controls/src/Core/Platform/iOS/ControlsNavigationController.cs +++ b/src/Controls/src/Core/Platform/iOS/ControlsNavigationController.cs @@ -159,15 +159,15 @@ internal void OnPushRequested(NavigationRequestedEventArgs e, IMauiContext mauiC void PushPage(IPage page, bool animated, IMauiContext mauiContext, TaskCompletionSource completionSource = null) { - var nativeView = page.ToNative(mauiContext); + var viewController = page.ToUIViewController(mauiContext); var handler = (INativeViewHandler)page.Handler; _trackers[page] = handler; if (completionSource != null) - _completionTasks[handler.ViewController] = completionSource; + _completionTasks[viewController] = completionSource; - PushViewController(handler.ViewController, animated); + PushViewController(viewController, animated); } diff --git a/src/Core/src/Handlers/Page/PageHandler.iOS.cs b/src/Core/src/Handlers/Page/PageHandler.iOS.cs index c252d18b8648..31c1aceddadb 100644 --- a/src/Core/src/Handlers/Page/PageHandler.iOS.cs +++ b/src/Core/src/Handlers/Page/PageHandler.iOS.cs @@ -22,12 +22,11 @@ protected override PageView CreateNativeView() _pageViewController = new PageViewController(VirtualView, this.MauiContext); - // This triggers LoadView - if (_pageViewController.View is PageView pv1) - return pv1; + if (_pageViewController.CurrentNativeView == null) + _pageViewController.LoadNativeView(); - if (_pageViewController.CurrentNativeView is PageView pv2) - return pv2; + if (_pageViewController.CurrentNativeView is PageView pv) + return pv; throw new InvalidOperationException($"PageViewController.View must be a PageView"); } diff --git a/src/Core/src/Platform/iOS/ContainerViewController.cs b/src/Core/src/Platform/iOS/ContainerViewController.cs index 3800918d3870..275942ce8849 100644 --- a/src/Core/src/Platform/iOS/ContainerViewController.cs +++ b/src/Core/src/Platform/iOS/ContainerViewController.cs @@ -28,10 +28,9 @@ void SetView(IView? view, bool forceRefresh = false) } currentNativeView?.RemoveFromSuperview(); currentNativeView = null; - if (IsViewLoaded && _view != null) + if (IsViewLoaded) { - _ = Context ?? throw new ArgumentNullException(nameof(Context)); - View!.AddSubview(currentNativeView = _view.ToNative(Context)); + LoadNativeView(); } } @@ -39,7 +38,16 @@ public override void LoadView() { base.LoadView(); if (_view != null && Context != null) - View!.AddSubview(currentNativeView = _view.ToNative(Context)); + LoadNativeView(); + } + + internal void LoadNativeView() + { + if (_view == null) + return; + + _ = Context ?? throw new ArgumentNullException(nameof(Context)); + View!.AddSubview(currentNativeView = _view.ToNative(Context)); } public override void ViewDidLayoutSubviews()