我编写了一个WPF程序,当用户点击一个按钮时,会弹出一个新窗口 .
我试图使用 Show() 或 ShowDialog() 函数显示新窗口 .
在 Windows 7 中,当用户关闭子窗口时,主窗口将保留,程序将不会退出 . 这种行为是我想要的 .
但是,当程序在 Windows XP 中运行时,当用户关闭子窗口时,主窗口将一起关闭,整个程序将退出 .
我试图在Window类的不同属性中设置不同的值,最后,我发现只有在子窗口中设置属性 "ShowInTaskbar" to "False" 时程序才会退出 .
但是,如果ShowInTaskbar设置为false,则用户无法在任务栏中找到不是我想要的行为的条目 .
我想拥有的非常简单 . 我只是希望在用户关闭子窗口时(即 main window will not exit when user closed the child window ),在Windows XP中运行的程序与在Windows 7中运行的程序具有相同的行为 . 另外,我想在任务栏中为新创建的子窗口输入一个条目(即 ShowInTaskbar = true ) .
有没有人对这个问题有任何想法?
MainWindow
<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>
Code For MainWindow:
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();
}
}
Child Window:
<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>
Code for Child Window:
public partial class ChildWindow : Window
{
public ChildWindow()
{
InitializeComponent();
}
}
2 回答
在调用
childWindow.ShowDialog()
之前,您是否确保将childWindow.Owner
设置为我们的MainWindow?根本不是一个优雅的解决方案,但您始终可以在 Application 类中订阅Closing事件并在事件处理程序中取消应用程序关闭 .