【问题标题】:WPF Child Windows problem in Windows XPWindows XP 中的 WPF 子窗口问题
【发布时间】:2023-03-08 15:12:01
【问题描述】:

我编写了一个 WPF 程序,当用户单击按钮时,会弹出一个新窗口。

我尝试使用 Show()ShowDialog() 函数显示新窗口。

Windows 7中,当用户关闭子窗口时,主窗口将保留,程序不会退出。这种行为是我想要的。

但是,当程序在Windows XP中运行时,当用户关闭子窗口时,主窗口将一起关闭,整个程序将退出。

我尝试在Window类的不同属性中设置不同的值,最后发现只有在子窗口中设置属性“ShowInTaskbar”为“False”时程序才不会退出 .

但是,如果 ShowInTaskbar 设置为 false,用户在任务栏中找不到条目,​​这不是我想要的行为。

我想要的真的很简单。我只是希望在 Windows XP 中运行的程序在用户关闭子窗口时具有与在 Windows 7 中运行的程序相同的行为(即 当用户关闭子窗口时主窗口不会退出)。另外,我想在任务栏中为新创建的子窗口添加一个条目(即 ShowInTaskbar = true)。

有人知道这个问题吗?

主窗口

<Window x:Class="ChildWindowTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Click="OpenChild">Open Child Window</Button>
</Grid>
</Window>

主窗口代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void OpenChild(object sender, RoutedEventArgs e)
    {
        ChildWindow child = new ChildWindow();
        child.Owner = this;
        //child.ShowInTaskbar = false; <--- if comment, the program will exit, when child window closed
        child.Show();
    }
}

子窗口:

<Window x:Class="ChildWindowTest.ChildWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ChildWindow" Height="300" Width="300">
<Grid>

</Grid>

子窗口代码:

public partial class ChildWindow : Window
{
    public ChildWindow()
    {
        InitializeComponent();
    }
}

【问题讨论】:

  • 你能添加简单的代码来重现这个问题吗? XP和WIN7我都用过,从来没有见过这样的行为...尝试用父子窗口创建一个非常简单的项目,看看它是否发生在那里

标签: wpf windows-xp


【解决方案1】:

根本不是一个优雅的解决方案,但您始终可以在 Application 类中订阅 Closing 事件并在事件处理程序中取消应用程序关闭。

【讨论】:

    【解决方案2】:

    在调用childWindow.ShowDialog() 之前,您是否确保将childWindow.Owner 正确设置为我们的主窗口?

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多