简体   繁体   English

如何关闭 WPF 中隐藏的主 window?

[英]How to close a hidden main window in WPF?

I have two windows in a single application.我在一个应用程序中有两个 windows。

The first window has one button.第一个 window 有一个按钮。 When I click the button, the second window opens.当我点击按钮时,第二个 window 打开。 (The first window needs to close or hide). (第一个 window 需要关闭或隐藏)。 Then I need to close my second window and need to stop the debugging.然后我需要关闭我的第二个 window 并需要停止调试。 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: MainWindow 代码隐藏:

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:

<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.您的问题似乎是您正在运行显示 window 的代码,可能是模态的。 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您尚未显示此代码,但如果您的 btnCreate 处理程序之前已关闭

secondWindow.ShowDialog();

You'd be trying to shut the app down effectively at the same time as showing that window as a dialog.您将尝试在将 window 显示为对话框的同时有效地关闭应用程序。

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.您想要的是关闭应用程序的代码,但不是在 UI 忙于执行其他操作(例如显示 window)时。

You could do that by moving your shutdown so it's in say the contentrendered event handler of that window you're showing.你可以通过移动你的关机来做到这一点,也就是说你正在展示的那个 window 的内容渲染事件处理程序。

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.在任何情况下,您都应该确保您没有显示另一个 window 或来自任何 windows 的 window.closure 事件的代码的代码。 Since closing the app will necessarily involve closing windows.由于关闭应用程序必然涉及关闭 windows。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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