maui
maui copied to clipboard
Modal navigation not working when the window is not active
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
- Create a new MAUI project.
- Create two views (MainPage and Page1) and two view models (MainPageViewModel and Page1ViewModel)
- Implement modal navigation in both view models (PushModalAsync and PopModalAsync)
- Launch the application and check it's working well: click on buttons to navigate from one page to the other.
- 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
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.
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
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.
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.
Any update on this issue?
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.
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.
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
Thank you for sharing your workaround!
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!