简体   繁体   中英

How to close a hidden main window in WPF?

I have two windows in a single application.

The first window has one button. When I click the button, the second window opens. (The first window needs to close or hide). Then I need to close my second window and need to stop the debugging. How to do it?

<Window x:Class="WpfApp5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp5"
    xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
    xmlns:Control="clr-namespace:Syncfusion.Windows.Tools.Controls;assembly=Syncfusion.Tools.Wpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Button Content="Create" Height="50" Width="100" Click="btncreate"></Button>
</Grid>

MainWindow code-behind:

public partial class MainWindow : Window
{
    public static TextBox textBox;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btncreate(object sender, RoutedEventArgs e)
    {
        SecondWindow secondWindow = new SecondWindow();
        secondWindow.Owner = this;
        this.Hide();
        secondWindow.ShowDialog();
    }
}

Second window:

<Window x:Class="WpfApp5.SecondWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp5"
    mc:Ignorable="d"
    Title="SecondWindow" Height="450" Width="800">
<Grid>
    <StackPanel>
        <TextBlock Text="New Project Loaded" HorizontalAlignment="Center" Margin="50"></TextBlock>
    </StackPanel>
</Grid>

Your problem seems to be that you're running code which is showing a window, maybe modally. At the same time, you're trying to shut the app down.

You've not shown this code but if your btnCreate handler has shutdown in it just before

secondWindow.ShowDialog();

You'd be trying to shut the app down effectively at the same time as showing that window as a dialog.

What you want is your code to shut the app down but not whilst the UI is busy doing something else like show a window.

You could do that by moving your shutdown so it's in say the contentrendered event handler of that window you're showing.

Another option is to schedule the shutdown using dispatcher.

Application.Current.Dispatcher.InvokeAsync(new Action(() => 
{  
    Application.Current.Shutdown();
}), DispatcherPriority.ContextIdle);

If you still have errors you could consider instead using.

      Environment.Exit(0);

This is a rather brute force approach compared to shutdown but if you're only doing this whilst debugging that probably won't matter.

In any case, you should ensure you don't have code which shows another window or something from the window.closing events of any of your windows. Since closing the app will necessarily involve closing windows.

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