本文介绍了Windows XP 中的 WPF 子窗口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a WPF program that when user clicked a button, a new window will be popped up.

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

I have tried to show the new window by using Show() or ShowDialog() function.

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

In Windows 7, when user closed the child window, the main window will remain and the program will not exit. This behavior is what I want to have.

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

However, when the program is run in Windows XP, when user closed the child window, the main window will be closed together and the whole program will be exited.

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

I have tried to set different value in different properties in Window class, finally, I found that the program will not exit only when I set the property "ShowInTaskbar" to "False" in child window.

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

However, if ShowInTaskbar is set to false, user cannot find the entry in task bar which is not the behavior that I want.

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

What I want to have is really simple. I just want the program running in Windows XP to have the same behavior as the program running in Windows 7 when user closed the child window (i.e. main window will not exit when user closed the child window). Also, I want to have an entry in task bar for a newly created child window(i.e. 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();
    }
}

推荐答案

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

Not an elegant solution at all, but you always can subscribe to Closing event in Application class and cancel application closing in an event handler.

这篇关于Windows XP 中的 WPF 子窗口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:57