How to show new window/dialog in WinUI3?

aluzi liu 486 Reputation points
2023-05-05T10:46:06.1366667+00:00

My project contains two window, login and main

After login success, I need to open main window and close login window,I try:

            var mainForm = new MainForm();
            mainForm.Activate();
            Close();

It seems works, but I am very confused, because in App.xaml.cs OnLaunched:

        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            m_window = new LoginForm();
            m_window.Activate();
        }

Do I have to set the m_window to newly opened MainForm?

But how to access the m_window?

And how to ShowDialog in WinUI3?

Windows development | Windows App SDK
Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2023-05-06T16:44:56.3633333+00:00

    You can first open the Login Form (Login here) :

            protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
            {
                m_window = new Login();
                m_window.Activate();
            }
    
    

    and in Login Form you can display the Main window when it is closed on OK Button (if User/Password is OK) :

            this.Closed += Login_Closed;
    
    
            private Window m_window;
            private void Login_Closed(object sender, WindowEventArgs args)
            {
                m_window = new MainWindow();
                m_window.Activate();
            }
    
    

    Test :

    TLogin

    1 person found this answer helpful.
    0 comments No comments

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.