简体   繁体   中英

Wpf c# how to close window in page?

I found the problem is It cannot close this window. But it can open the MainWindow. pls help在此处输入图像描述 Code in button

        private void LoginBtn_Click(object sender, RoutedEventArgs e)
        {
            MainWindow MainView = new MainWindow();
            MainView.Show();

            AuthWindow AuthView = new AuthWindow();
            AuthView.Close();

        }

I want to press the button inside the page and close that window and open another window.

For such scenarios, I advise you to use RoutedCommand. In this case, you can use the ready-made command ApplicationCommands.Close .

In the page button, specify the command name:

    <Button Content="Close Window" Command="Close" />

In the Window, set command executing:

    <Window.CommandBindings>
        <CommandBinding Command="Close" Executed="OnCloseWindow"/>
    </Window.CommandBindings>
    <x:Code>
        <![CDATA[
    private void OnCloseWindow(object sender, ExecutedRoutedEventArgs e)
    {
        this.Close();
    }
        ]]>
    </x:Code>

PS I also do not advise you to open new windows. Since you are using Pages, you should change Pages in a single Window. And closing the Window is regarded as an Exit from the Application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM