How to activate a window in .net maui on Mac Catalyst

Ramin Khalili 60 Reputation points
2023-11-02T09:43:06.6166667+00:00

I have a .Net Maui Desktop application working on mac catalyst with multiple windows.

I want to Activate (focus, bring to front) a secondary window when its under my primary window.

(To do this under windows see : https://learn.microsoft.com/en-us/answers/questions/1411868/how-to-activate-a-window-in-net-maui)

Lets say a Button in my primary window opens a secondary window, now the users click on the primary window and clicks the same button again, instead of opening the secondary window multiple times I would like to bring it to the foreground if its already opened.

I didn't find any methods in in the Window class, not any in Application.Current to do this.

Here is the simplified code for the Button Clicked function:

private void MyButtonClicked(object sender, EventArgs e)
{
	if (SecondWindowCreated == false)
	{
		SecondWindow = new Window(SecondWindowPage);
		SecondWindow.Created += (s, e) => { SecondWindowCreated = true; };
		SecondWindow.Destroying += (s, e) => { SecondWindowCreated = false; };
		Application.Current.OpenWindow(SecondWindow);
	}
	else
		// I need to do something to Activate (bring to front) SecondWindow
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,870 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,336 Reputation points Microsoft Vendor
    2023-11-03T08:49:02.4933333+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful