Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PopupExtensions.shared.cs #1229

Merged
merged 1 commit into from
Jun 13, 2023

Conversation

cat0363
Copy link
Contributor

@cat0363 cat0363 commented Jun 9, 2023

When I try to show the Popup by calling the PopupShow method inside the NavigatedTo event, it doesn't show up.
Same if I try to show the Popup in the OnAppearing event. I was getting the below exception message.

Could not locate MauiContext.

The location where the exception occurred is the code below called in the CreatePopup method of PopupExtensions.shared.cs.

var mauiContext = GetMauiContext(page);

The Get MauiContext method was implemented like this:

return page.Handler?.MauiContext ?? throw new InvalidOperationException("Could not locate MauiContext.");

The page's Handler is null during the OnAppearing and OnNavigatedTo event timings.
Therefore, MauiContext cannot be obtained. The timing that the Handler of the page can
acquire is the Loaded event.

The implementation before the fix calls CreateAndShowPopup method in the NavigatedTo event.

When the Popup is displayed in the OnAppearing event, the Handler of the page is null, so the exception above is raised.

When the Popup is displayed in the OnNavigatedTo event, the CreateAndShowPopup method is not called because the registration for the NavigatedTo event is performed within the Page's OnNavigatedTo event.

So I changed the CreateAndShowPopup method to be called in the Loaded event instead of the NavigatedTo event.
The Loaded event is a later event than the OnAppearing and OnNavigatedTo events, and the page's Handler is never null.

Description of Change

Linked Issues

PR Checklist

Additional information

page.IsPlatformEnabled is True only in the Loaded event. OnAppearing event, OnNavigatedTo event,
page.IsPlatformEnabled is False. In the OnLoaded event the CreateAndShowPopup method is called
directly, but in the OnAppearing and OnNavigatedTo events it is called within the Loaded event.

Below is the execution result.

[Show the popup inside the OnAppearing event on Android]

Android.Emulator.-.pixel_2_-_api_30_5554.2023-06-09.12-02-19.mp4

[Show the popup inside the OnNavigatedTo event on Android]

Android.Emulator.-.pixel_2_-_api_30_5554.2023-06-09.12-03-45.mp4

[Show the popup inside the Loaded event on Android]

Android.Emulator.-.pixel_2_-_api_30_5554.2023-06-09.12-05-32.mp4

[Show the popup inside the OnAppearing event on iOS]

A_RPReplay_Final1686281776.MP4

[Show the popup inside the OnNavigatedTo event on iOS]

N_RPReplay_Final1686281148.MP4

[Show the popup inside the Loaded event on iOS]

L_RPReplay_Final1686280764.MP4

@cat0363
Copy link
Contributor Author

cat0363 commented Jun 9, 2023

@dotnet-policy-service agree

Copy link
Contributor

@bijington bijington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding my approval to say thanks for this effort!

@VladislavAntonyuk VladislavAntonyuk enabled auto-merge (squash) June 13, 2023 10:22
@VladislavAntonyuk VladislavAntonyuk merged commit b62e2e4 into CommunityToolkit:main Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Popup not showing in NavigatedTo event on Shell.
3 participants