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

Alerts do not display from ctor or OnAppearing when not in a NavPage/Shell #12970

Open
mattleibow opened this issue Jan 27, 2023 · 12 comments
Open
Labels
area-controls-dialogalert DisplayAlert, dialog platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@mattleibow
Copy link
Member

Description

Seems that the alerts are fired correctly and then added to the _pendingActions. However, the FlushPendingActions method only fires when the NavigatedTo even is invoked - and this only happens when in a nav page or in shell.

Steps to Reproduce

  1. In the sandbox app
  2. Use this code in the MauiProgram.cs:
	class App : Application
	{
		protected override Window CreateWindow(IActivationState activationState) =>
			new Window(new MainPage());
	}
  1. Use this code in the MainPage.xaml.cs:
	protected override void OnAppearing()
	{
		base.OnAppearing();
		DisplayAlert("A", "B", "C");
	}

Link to public reproduction project repository

maui repo

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

All version as this is xplat code

Did you find any workaround?

No response

Relevant log output

No response

@mattleibow mattleibow added the t/bug Something isn't working label Jan 27, 2023
@mattleibow
Copy link
Member Author

@PureWeen not sure the overall navigation / alerts plan, but saw this while trying to create tests for #12910

@jsuarezruiz jsuarezruiz added the area-controls-dialogalert DisplayAlert, dialog label Jan 30, 2023
@ghost ghost added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Jan 30, 2023
@jsuarezruiz jsuarezruiz added area-controls-shell Shell Navigation, Routes, Tabs, Flyout and removed legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor labels Jan 30, 2023
@mattleibow mattleibow removed the area-controls-shell Shell Navigation, Routes, Tabs, Flyout label Jan 30, 2023
@jsuarezruiz
Copy link
Contributor

Same or similar to #12739

@jsuarezruiz jsuarezruiz added the s/duplicate 2️⃣ This issue or pull request already exists label Feb 9, 2023
@jsuarezruiz jsuarezruiz added this to the Backlog milestone Feb 9, 2023
@ghost
Copy link

ghost commented Feb 9, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@Eilon Eilon added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Mar 2, 2023
@samhouts samhouts removed the s/duplicate 2️⃣ This issue or pull request already exists label May 10, 2023
@ShariatPanah
Copy link

any update on this guys? i have the same problem

@jeremy-visionaid
Copy link
Contributor

Yup, I just hit this one too.

@jeremy-visionaid
Copy link
Contributor

jeremy-visionaid commented Sep 18, 2023

@mattleibow I find this to be even worse than the title...

On Windows, OnNavigatedTo actually fires in my app, but the alert is still not displayed. If you try to work around it by adding an (insufficient) delay by using a timer or a Task.Delay, then it can cause a crash (8.0.0-rc.1.9171):

   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr) in WinRT\ExceptionHelpers.cs:line 103
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr) in WinRT\ExceptionHelpers.cs:line 105
   at ABI.Microsoft.UI.Xaml.Controls.IContentDialogMethods.ShowAsync(IObjectReference _obj) in ABI.Microsoft.UI.Xaml.Controls\IContentDialogMethods.cs:line 51
   at Microsoft.UI.Xaml.Controls.ContentDialog.ShowAsync() in Microsoft.UI.Xaml.Controls\ContentDialog.cs:line 527
   at Microsoft.Maui.Controls.Platform.AlertManager.AlertRequestHelper.<ShowAlert>d__14.MoveNext() in Microsoft.Maui.Controls.Platform\AlertManager.cs:line 209
   at Microsoft.Maui.Controls.Platform.AlertManager.AlertRequestHelper.<OnAlertRequested>d__11.MoveNext() in Microsoft.Maui.Controls.Platform\AlertManager.cs:line 94
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) in /_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:line 1914
   at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()

So, seems like there's a pretty big problem in how to know how when it's valid to display an alert... I can put in a delay of a second, and that works OK on my machine (anything much shorter is unreliable) - but I don't know if it's sufficient for other cases.

@homeyf homeyf added s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage labels Sep 26, 2023
@homeyf
Copy link

homeyf commented Sep 26, 2023

Verified this issue with Visual Studio Enterprise 17.8.0 Preview 2.0. Can repro on windows platform with sample code.

@jeremy-visionaid
Copy link
Contributor

Just to note here from #12739 that the Page.Loaded event can be hooked instead as a workaround

@Mark-NC001
Copy link

This is quite a big issue for me, as I have Try...Catches in OnAppearing that call DisplayAlert to let the user know of issues.

@jeremy-visionaid
Copy link
Contributor

@Mark-NC001 Coud you queue it on the dispatcher as a workaround? I was able to reorganize to avoid the problem deterministaically via Loaded, but in my initial attempt I found I needed quite a long delay before it worked (maybe 2-3 seconds on the first OnAppearing invocation)

@kubaflo
Copy link
Contributor

kubaflo commented Sep 19, 2024

Hi, @Mark-NC001 try this as a workaround

	protected override void OnAppearing()
	{
		base.OnAppearing();
		DisplayAlertOnAppearing("Hello","Message","Ok");
	}

	void DisplayAlertOnAppearing(string title, string message, string cancel) 
	{
		Loaded += OnLoaded;
		void OnLoaded(object sender, EventArgs e)
		{
			DisplayAlert(title, message, cancel);
			Loaded -= OnLoaded;
		}
	}

@Mark-NC001
Copy link

@kubaflo and @jeremy-visionaid thanks for the info. In the end I've made a new function that I call from within OnAppearing, waiting until it's done. I'm preparing myself for the many reasons why I shouldn't do this.....!

public static async Task EnsurePageLoaded(Page page, int intervalMilliseconds = 100, int maxWaitMilliseconds = 6000)
{
		if (page == null) throw new ArgumentNullException(nameof(page));

		int maxWaited = 0;

		while (!page.IsLoaded)
		{
			if (maxWaited > maxWaitMilliseconds)
				throw new InvalidOperationException("Waited " + maxWaited + " milliseconds, but page still not loaded!");

			maxWaited += intervalMilliseconds;
			await Task.Delay(intervalMilliseconds);
		}
}

Also, it's not just DisplayAlerts that aren't working - I'm also using dependency injection:
Application.Current.MainPage.Handler.MauiContext.Services.GetService()
and Handler appears to be null, until the page has loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-dialogalert DisplayAlert, dialog platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants