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.