任务栏上的 C# wpf 多个 windows
C# wpf multiple windows on task bar
我创建了一个包含多个 windows 的 C# 项目。当我做一些其他事情时,一些 windows 失去了焦点。然后我必须通过单击任务栏将它们一个一个地放在前面。
我想知道 C# wpf 上是否有 OnTaskBarClick 事件 window?
然后我需要做的是将所有子 windows 设置为 "ShowInTaskbar="False""。然后,当我在任务栏上单击 mainwindow 时,我会将所有 windows 置于最前面。
您需要使用 Window.Activated 事件来检测应用程序的 window 何时进入焦点:
来自文档:
A window is activated (becomes the foreground window) when:
The window is first opened.
A user switches to a window by selecting it with the mouse, pressing ALT+TAB, or from Task Manager.
A user clicks the window's taskbar button.
或者你可以使用Application.Activated事件。
点击已经激活的 window 的控件也可能导致 Window.Activated
事件触发,所以如果问题是它触发得太频繁,您可能需要观察当 应用程序 在活动和停用之间切换时。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Application.Current.Activated += CurrentOnActivated;
Application.Current.Deactivated += CurrentOnDeactivated;
}
private bool isDeactivated = true;
private void CurrentOnDeactivated(object sender, EventArgs eventArgs)
{
isDeactivated = true;
//handle case where another app gets focus
}
private void CurrentOnActivated(object sender, EventArgs eventArgs)
{
if (isDeactivated)
{
//Ok, this app was in the background but now is in the foreground,
isDeactivated = false;
//TODO: bring windows to forefont
MessageBox.Show("activated");
}
}
}
我认为您遇到的是 window Owner
未在 child window 上设置。
When a child window is opened by a parent window by calling
ShowDialog, an implicit relationship is established between both
parent and child window. This relationship enforces certain behaviors,
including with respect to minimizing, maximizing, and restoring.
-
When a child window is created by a parent window by calling Show,
however, the child window does not have a relationship with the parent
window. This means that:
The child window does not have a reference to the parent window.
The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be
minimized, maximized, and restored independently of the other.
您可以通过在调用 Show()
或 ShowDialog()
child window 中设置 Owner
属性 轻松解决此问题
Window ownedWindow = new Window();
ownedWindow.Owner = this; // this being the main or parent window
ownedWindow.Show();
Note : if conforming to an MVVM pattern, this can become a little
more cumbersome, however there is plenty of resources on how to link owner and parent windows.
我创建了一个包含多个 windows 的 C# 项目。当我做一些其他事情时,一些 windows 失去了焦点。然后我必须通过单击任务栏将它们一个一个地放在前面。
我想知道 C# wpf 上是否有 OnTaskBarClick 事件 window?
然后我需要做的是将所有子 windows 设置为 "ShowInTaskbar="False""。然后,当我在任务栏上单击 mainwindow 时,我会将所有 windows 置于最前面。
您需要使用 Window.Activated 事件来检测应用程序的 window 何时进入焦点:
来自文档:
A window is activated (becomes the foreground window) when:
The window is first opened.
A user switches to a window by selecting it with the mouse, pressing ALT+TAB, or from Task Manager.
A user clicks the window's taskbar button.
或者你可以使用Application.Activated事件。
点击已经激活的 window 的控件也可能导致 Window.Activated
事件触发,所以如果问题是它触发得太频繁,您可能需要观察当 应用程序 在活动和停用之间切换时。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Application.Current.Activated += CurrentOnActivated;
Application.Current.Deactivated += CurrentOnDeactivated;
}
private bool isDeactivated = true;
private void CurrentOnDeactivated(object sender, EventArgs eventArgs)
{
isDeactivated = true;
//handle case where another app gets focus
}
private void CurrentOnActivated(object sender, EventArgs eventArgs)
{
if (isDeactivated)
{
//Ok, this app was in the background but now is in the foreground,
isDeactivated = false;
//TODO: bring windows to forefont
MessageBox.Show("activated");
}
}
}
我认为您遇到的是 window Owner
未在 child window 上设置。
When a child window is opened by a parent window by calling ShowDialog, an implicit relationship is established between both parent and child window. This relationship enforces certain behaviors, including with respect to minimizing, maximizing, and restoring.
-
When a child window is created by a parent window by calling Show, however, the child window does not have a relationship with the parent window. This means that:
The child window does not have a reference to the parent window.
The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be minimized, maximized, and restored independently of the other.
您可以通过在调用 Show()
或 ShowDialog()
child window 中设置 Owner
属性 轻松解决此问题
Window ownedWindow = new Window();
ownedWindow.Owner = this; // this being the main or parent window
ownedWindow.Show();
Note : if conforming to an MVVM pattern, this can become a little more cumbersome, however there is plenty of resources on how to link owner and parent windows.