detect open windows and bring to front, and open all window in in front.

Eduardo Gomez Romero 1,355 Reputation points
2024-04-01T23:09:22.8933333+00:00

I am making an app for windows and mac,

I made a ribbon menu.

I have a dictionary, where I have a Button Image associated with a particular page.

public partial class MainViewModel : ObservableObject {

    Dictionary<string, Page> _items = new() {

        {"mario.png", new Page1()},
        {"dotnet_bot.png", new Page_2()},
    };

    [RelayCommand]
    public void MenuItemSelected(object obj) {

        var imageButton = obj as ImageButton;

        var fileImageSource = imageButton?.Source as FileImageSource;

        var name = fileImageSource?.File;

        WindowHelper.CreateWindow(_items[name!], 300, 300);
    }


Also I declared a method to create new Windows

public static class WindowHelper {

    public static void CreateWindow(Page view, double height, double width, double x = 0, double y = 0) {

        var displayInfo = DeviceDisplay.MainDisplayInfo;

        if (height == 0 && width == 0) {

            width = displayInfo.Width;
            height = displayInfo.Height;
        }

        var newWindow = new Window() {

            Page = view,

            Height = height,

            Width = width,

            X = x,

            Y = y,

        };

        Application.Current?.OpenWindow(newWindow);
    }

this work as expected

User's image

but when I open other windows, that window go to the back

User's image

I want all my windows visible at the sati time (like in windows forms)

also, if window is open already, just bring it to the front, so don't open a new instance.

Developer technologies .NET .NET MAUI
{count} votes

Accepted answer
  1. Anonymous
    2024-04-02T07:16:37.7266667+00:00

    Hello,

    if window is open already, just bring it to the front, so don't open a new instance.

    For windows, Microsoft.Maui.Controls.Window do not have Activate() method in MAUI.

    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.

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

    You need to add a Title for your newWindow.

     var SecondWindowPage = new Window()
            {
               
                Title ="newWIndow",
    
                Page = view,
    
                Height = height,
    
                Width = width,
    
                X = x,
    
                Y = y,
    
    
           };
    

    Then, you need to add a flag to mark whether the window is open. If you want to make the window to the front, you can call MauiProgram.secondWindow.Activate(); directly.

      if (SecondWindowCreated == false)
    
            {
    
                SecondWindow = SecondWindowPage;
    
                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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.