简体   繁体   English

在 MessageBox 之后最小化 Window

[英]Minimizing Window after MessageBox

I am developing a little applicacion, using Visual Studio 2022, together with .NET 6.0我正在使用 Visual Studio 2022 和 .NET 6.0 开发一个小应用程序

I have a MainWindow and attached a subordinate Window which I am showing as follows:我有一个 MainWindow 并附加了一个从属 Window 我显示如下:

private void s_Betrieb_Click(object sender, RoutedEventArgs e)
{
    BetriebWindow betriebWindow = new();
    betriebWindow.Owner=this;
    betriebWindow.Show();
}

The subordinate Window BetriebWindow is an ordinary Window, defined in XAML, whitch shows an input-Form (using textBoxes and labels).从属 Window BetriebWindow 是一个普通的 Window,定义在 XAML 中,显示一个输入表单(使用文本框和标签)。

After having changed some input, the user clicks on "Close" (the red x in the right upper edge of the Window).更改某些输入后,用户单击“关闭”(窗口右上边缘的红色 x)。 I am asking the user wheather he would like to save his changes or not, using a MessageBox.Show().我问用户是否愿意使用 MessageBox.Show() 保存他的更改。

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (MessageBox.Show("Wollen Sie Ihre ",...) == MessageBoxResult.OK)
            SaveData();
        Close();
    }

After the subordinate Window has closed, the MainWindow is minimizing itself and appears in the taskbar, which I (and perhaps even the future user) don't like.在从属 Window 关闭后,MainWindow 正在最小化并出现在任务栏中,我(甚至可能是未来的用户)不喜欢。

What am I doing wrong?我究竟做错了什么? I want the MainWindow to remain opened on the screen.我希望 MainWindow 在屏幕上保持打开状态。 But closing the MainWindow should close the subordinate window, too.但是关闭 MainWindow 也应该关闭从属 window。

Thanks for an answer!感谢您的回答!

Paul保罗

Remove the call to Close() in the Closing handler and call Activate() on the main window when the child window has been closed:当子 window 已关闭时,删除Closing处理程序中对Close()的调用,并在主 window 上调用Activate()

private void s_Betrieb_Click(object sender, RoutedEventArgs e)
{
    BetriebWindow betriebWindow = new();
    betriebWindow.Owner = this;
    betriebWindow.Closed += (ss, ee) => Activate();
    betriebWindow.Show();
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (MessageBox.Show("Wollen Sie Ihre ", ...) == MessageBoxResult.OK)
        SaveData();
}

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

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