How to activate a window in .net maui

Ramin Khalili 60 Reputation points
2023-11-01T07:50:15.2666667+00:00

Hi All

I have a .Net Maui Desktop application (windows, mac catalyst) with multiple windows.

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

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 how my Button Clicked functions looks like:

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. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,651 Reputation points Microsoft Vendor
    2023-11-02T06:32:00.5266667+00:00

    Hello,

    Microsoft.Maui.Controls.Window do not have Activate() method in MAUI, please add a feature request in the MAUI GitHub repro.

    For windows, here is a workaround. when you create a window events.AddWindows(windows => windows.OnWindowCreated will be execute. And you can get the MauiWinUIWindow from the window. MauiWinUIWindow have Activate method. You can get the needed window by the title. Then set it the public static MauiWinUIWindow secondWindow, here is my MauiProgram.cs code.

     public static MauiWinUIWindow secondWindow;
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>().ConfigureLifecycleEvents(events =>
                    {
    #if WINDOWS
                        events.AddWindows(windows => windows.OnWindowCreated(window => {
                            var mauiWinUIWindow = window as MauiWinUIWindow;
                            if(mauiWinUIWindow.Title== "second") {
                                secondWindow= mauiWinUIWindow;
                            }
                        }));
    
    
    #endif
    

    If you want to make the window to the front, you can call MauiProgram.secondWindow.Activate(); directly.

     bool SecondWindowCreated;
            private void MyButtonClicked(object sender, EventArgs e)
            {
                Window SecondWindow;
                if (SecondWindowCreated == false)
                {
                    SecondWindow = new Window(new NewPage1());
                    SecondWindow.Title = "second";
                    SecondWindow.Created += (s, e) => { SecondWindowCreated = true; };
                    SecondWindow.Destroying += (s, e) => { SecondWindowCreated = false; };
                    Application.Current.OpenWindow(SecondWindow);
    
    
               }
                else
                {
    #if WINDOWS             
                    MauiProgram.secondWindow.Activate();
    #endif
                }
    

    Best Regards,

    Leon Lu


    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 additional answers

Sort by: Most helpful