Hello,
I would like to bring it to the foreground if its already opened.
First of all, I would like to inform you that the user has to touch the screen to active a session on iOS/MacCatalyst. Please watch the Apple's Window Management in Your Multitasking App video around 4:50. Therefore, you cannot bring an existing window to the foreground programmatically.
instead of opening the secondary window multiple times
For this, you could find the UserActivity of the second window, and then call UIApplication.SharedApplication.RequestSceneSessionActivation method on MacCatalyst platform.
(This method was deprecated in iOS17/Mac Catalyst 17, but iOS17/Mac Catalyst17 APIs are not fully supported with MAUI for now)
Please refer to the following code:
private void MyButtonClicked(object sender, EventArgs e)
{
var secondpage = new SecondWindowPage();// get the ContentPage
var SecondWindow = new Window(secondpage);
if (SecondWindowCreated == false)
{
SecondWindow.Created += (s, e) => { SecondWindowCreated = true; };
SecondWindow.Destroying += (s, e) => { SecondWindowCreated = false; };
Application.Current.OpenWindow(SecondWindow);
}
else
{
#if MACCATALYST
UIView uIView = secondpage.Handler.PlatformView as UIView;// get the native UIView according to the ContentPage's handler
UIApplication.SharedApplication.RequestSceneSessionActivation(
sceneSession: null,
userActivity: uIView.Window.WindowScene.UserActivity,// get the UserActivity
options: null,
errorHandler: null
);
#endif
}
}
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.