maui icon indicating copy to clipboard operation
maui copied to clipboard

Modal navigation not working when the window is not active

Open jafadie opened this issue 1 year ago • 10 comments

Description

When I use modal navigation and click outside the application window, the navigation stops working.

When the application window is active, modal navigation works correctly.

This application is composed of two views: MainPage and Page1.

I use the PushModalAsync method to navigate from MainPage to Page1 and I use the PopModalAsync method to come back to MainPage.

Steps to Reproduce

  1. Create a new MAUI project.
  2. Create two views (MainPage and Page1) and two view models (MainPageViewModel and Page1ViewModel)
  3. Implement modal navigation in both view models (PushModalAsync and PopModalAsync)
  4. Launch the application and check it's working well: click on buttons to navigate from one page to the other.
  5. Click the button to navigate and unfocus the window application (there is a 3 second-delay before navigating) and you will see that navigation is not working anymore.

Link to public reproduction project repository

https://github.com/jafadie/MadSense.MauiNavigation

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

https://github.com/dotnet/maui/issues/18982#issuecomment-2021195256

Relevant log output

No response

jafadie avatar Nov 23 '23 13:11 jafadie

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

ghost avatar Dec 04 '23 15:12 ghost

Verified this on Visual Studio Enterprise 17.9.0 Preview 2(8.0.3). Repro on Windows 11, not repro on MacCatalyst with below Project: Prismanda.zip

XamlTest avatar Dec 28 '23 10:12 XamlTest

I'd like to provide further insight into the previously reported navigation issue on Windows. We've observed that the application's modal navigation exhibits unpredictable behavior when the application window is not in focus or active. Notably, if the user shifts focus away from the app during an asynchronous operation, it results in a complete breakdown of the navigation functionality.

This behavior seems to occur randomly and disrupts the user experience significantly. Any guidance or suggestions to resolve this would be greatly appreciated.

YZahringer avatar Jan 05 '24 20:01 YZahringer

I have the same problem. There is a situation where the external payment program has to take the focus while the MAUI APP is running.

I hope this issue will be resolved soon.

kasanhoon avatar Jan 24 '24 07:01 kasanhoon

Any update on this issue?

ivosnoza avatar Jan 25 '24 09:01 ivosnoza

I'm also having issues with this. Doing a Focus() on the Page about to perform the PopModalAsync operation seems to remedy the problem. At least in some situations.

Brosten avatar Mar 04 '24 05:03 Brosten

Can also confirm this issue exists in our app but only on Windows. When the MAUI window loses focus, the await Navigation.PopModalAsync() is never fired at the end of our processing of user data by the application. However, if we keep the window in focus/active, all works as expected.

jayhayman-hdd avatar Mar 26 '24 17:03 jayhayman-hdd

Thanks to @Brosten for putting me on the right track, I have a dirty hack.

In my situation, if the Window is also minimised (goes into OnSleep), just using Focus() itself isn't enough and I have to Restore the app then use Focus() to dismiss the modal.

Focus() is the key here to wake up the async navigation event...

	// STOP: End App and UGC update.
#if WINDOWS
	// Code smell dev.0041a1: Dirty hack for Windows to return focus to the MAUI main window by "Restoring" the window if it was minimised
	// and adding "Focus" if the window is not minimised but also out of focus.
	// TODO: Wait for MAUI bug fix then remove.
	var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow;
	var appWindow = GetAppWindow(window);
	switch (appWindow.Presenter)
        {
            case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:
                //overlappedPresenter.SetBorderAndTitleBar(false, false);
                overlappedPresenter.Restore();
                break;
        }

	Focus();
#endif
	await Navigation.PopModalAsync(animated: true);

With this function to capture the Window handle:

#if WINDOWS
    // Code smell dev.0041a2: Dirty hack for Windows to return focus to the MAUI main window by "Restoring" the window if it was minimised
    // and adding "Focus" if the window is not minimised but also out of focus.
    // TODO: Wait for MAUI bug fix then remove.
    private Microsoft.UI.Windowing.AppWindow GetAppWindow(MauiWinUIWindow window)
    {
        var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
        var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
        var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
        return appWindow;
    }
#endif

jayhayman-hdd avatar Mar 26 '24 18:03 jayhayman-hdd

Thank you for sharing your workaround!

samhouts avatar Apr 09 '24 00:04 samhouts

I closed the issue https://github.com/dotnet/maui/issues/21158 as it is a duplicate. Thankfully @jayhayman-hdd work around is working for me currently. I hope this issue gets fixed soon though.

Thanks Jay!

Kbmobi avatar Apr 17 '24 01:04 Kbmobi