Windows XP 中的 WPF 子 Windows 问题

发布于 2024-12-05 07:23:38 字数 1983 浏览 1 评论 0原文

我编写了一个 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();
    }
}

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

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

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.

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.

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.

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

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).

Does anyone have any idea about this problem?

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满身野味 2024-12-12 07:23:38

根本不是一个优雅的解决方案,但您始终可以在 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.

茶底世界 2024-12-12 07:23:38

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

Did u make sure you have childWindow.Owner set as our MainWindow correctly before calling childWindow.ShowDialog()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文