diff --git a/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs b/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs index 0c4d97c8a..8173f266f 100644 --- a/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs @@ -114,6 +114,7 @@ public partial class AppShell : Shell CreateViewModelMapping(), CreateViewModelMapping(), CreateViewModelMapping(), + CreateViewModelMapping(), }); public AppShell() => InitializeComponent(); diff --git a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs index ead33e734..e74166826 100644 --- a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs +++ b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs @@ -167,6 +167,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services) services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); + services.AddTransientWithShellRoute(); // Add Popups services.AddTransient(); diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml new file mode 100644 index 000000000..d12c8c75e --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml @@ -0,0 +1,16 @@ + + + + + diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml.cs new file mode 100644 index 000000000..89cd86a40 --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml.cs @@ -0,0 +1,26 @@ +using CommunityToolkit.Maui.Sample.Models; +using CommunityToolkit.Maui.Sample.ViewModels.Views; +using CommunityToolkit.Maui.Views; + +namespace CommunityToolkit.Maui.Sample.Pages.Views; + +public partial class ShowPopupInOnAppearingPage : BasePage +{ + readonly PopupSizeConstants popupSizeConstants; + + public ShowPopupInOnAppearingPage( + PopupSizeConstants popupSizeConstants, + ShowPopupInOnAppearingPageViewModel showPopupInOnAppearingPageViewModel) + : base(showPopupInOnAppearingPageViewModel) + { + InitializeComponent(); + this.popupSizeConstants = popupSizeConstants; + } + + protected override async void OnAppearing() + { + + // Proves that we now support showing a popup before the platform is even ready. + var result = await this.ShowPopupAsync(new ReturnResultPopup(popupSizeConstants)); + } +} diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/ShowPopupInOnAppearingPageViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/ShowPopupInOnAppearingPageViewModel.cs new file mode 100644 index 000000000..dbff1f118 --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/ShowPopupInOnAppearingPageViewModel.cs @@ -0,0 +1,5 @@ +namespace CommunityToolkit.Maui.Sample.ViewModels.Views; + +public class ShowPopupInOnAppearingPageViewModel : BaseViewModel +{ +} diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs index 78f09a748..c37b93918 100644 --- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs @@ -25,6 +25,7 @@ public ViewsGalleryViewModel() SectionModel.Create("Mutiple Popups Page", Colors.Red, "A page demonstrating multiple different Popups"), SectionModel.Create("Custom Positioning Popup", Colors.Red, "Displays a basic popup anywhere on the screen using VerticalOptions and HorizontalOptions"), SectionModel.Create("Anchor Popup", Colors.Red, "Popups can be anchored to other view's on the screen"), + SectionModel.Create("Show Popup in OnAppearing", Colors.Red, "Proves that we now support showing a popup before the platform is even ready."), }) { } diff --git a/src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs b/src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs index 66a9186e4..78c20fc0b 100644 --- a/src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs +++ b/src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs @@ -21,12 +21,21 @@ public static partial class PopupExtensions /// public static void ShowPopup(this Page page, TPopup popup) where TPopup : Popup { -#if WINDOWS - PlatformShowPopup(popup, GetMauiContext(page)); -#else - CreatePopup(page, popup); -#endif + if (page.IsPlatformEnabled) + { + CreateAndShowPopup(page, popup); + } + else + { + void handler(object? sender, NavigatedToEventArgs args) + { + page.NavigatedTo -= handler; + CreateAndShowPopup(page, popup); + } + + page.NavigatedTo += handler; + } } /// @@ -43,13 +52,34 @@ public static void ShowPopup(this Page page, TPopup popup) where TPopup /// public static Task ShowPopupAsync(this Page page, TPopup popup) where TPopup : Popup { -#if WINDOWS - return PlatformShowPopupAsync(popup, GetMauiContext(page)); -#else + if (page.IsPlatformEnabled) + { + return CreateAndShowPopupAsync(page, popup); + } + else + { + var taskCompletionSource = new TaskCompletionSource(); - CreatePopup(page, popup); - return popup.Result; -#endif + async void handler(object? sender, NavigatedToEventArgs args) + { + page.NavigatedTo -= handler; + + try + { + var result = await CreateAndShowPopupAsync(page, popup); + + taskCompletionSource.TrySetResult(result); + } + catch (Exception ex) + { + taskCompletionSource.TrySetException(ex); + } + } + + page.NavigatedTo += handler; + + return taskCompletionSource.Task; + } } static void CreatePopup(Page page, Popup popup) @@ -65,4 +95,24 @@ static IMauiContext GetMauiContext(Page page) { return page.Handler?.MauiContext ?? throw new InvalidOperationException("Could not locate MauiContext."); } + + static void CreateAndShowPopup(Page page, TPopup popup) where TPopup : Popup + { +#if WINDOWS + PlatformShowPopup(popup, GetMauiContext(page)); +#else + CreatePopup(page, popup); +#endif + } + + static Task CreateAndShowPopupAsync(this Page page, TPopup popup) where TPopup : Popup + { +#if WINDOWS + return PlatformShowPopupAsync(popup, GetMauiContext(page)); +#else + CreatePopup(page, popup); + + return popup.Result; +#endif + } } \ No newline at end of file