Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 7443f05

Browse files
Fix NullReferenceException thrown in PopupRenderer.SetViewController (#1762)
* NullReferenceException thrown in PopupRenderer.SetViewController when using in embedded forms scenario * remove Null Forgiving Operator * Fix Null Forgiving Operator * Fix Null Forgiving Operator
1 parent 999f86b commit 7443f05

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Popup/iOS/PopupRenderer.ios.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public PopupRenderer()
3737
{
3838
}
3939

40+
[Preserve(Conditional = true)]
41+
public PopupRenderer(UIViewController viewController)
42+
{
43+
ViewController = viewController;
44+
}
45+
4046
public void SetElementSize(Size size) =>
4147
Control?.SetElementSize(size);
4248

@@ -134,18 +140,27 @@ void CreateControl()
134140
void SetViewController()
135141
{
136142
IVisualElementRenderer currentPageRenderer;
137-
var modalStackCount = Application.Current.MainPage?.Navigation?.ModalStack?.Count ?? 0;
138-
var mainPage = Application.Current.MainPage;
139-
if (modalStackCount > 0)
143+
var page = Application.Current.MainPage;
144+
var modalStackCount = page?.Navigation.ModalStack.Count ?? 0;
145+
if (modalStackCount > 0 && page is not null)
140146
{
141147
var index = modalStackCount - 1;
142-
currentPageRenderer = Platform.GetRenderer(mainPage!.Navigation!.ModalStack![index]);
148+
page = page.Navigation.ModalStack[index];
149+
currentPageRenderer = Platform.GetRenderer(page);
150+
}
151+
else
152+
{
153+
currentPageRenderer = Platform.GetRenderer(page);
154+
}
155+
156+
if (currentPageRenderer == null)
157+
{
158+
ViewController ??= page?.CreateViewController();
143159
}
144160
else
145161
{
146-
currentPageRenderer = Platform.GetRenderer(mainPage);
162+
ViewController ??= currentPageRenderer.ViewController;
147163
}
148-
ViewController = currentPageRenderer.ViewController;
149164
}
150165

151166
void SetEvents()

0 commit comments

Comments
 (0)