WPF:即使显式设置 WindowState,窗口仍保持最小化
我的应用程序有一个托盘图标,双击该图标可隐藏或显示应用程序窗口。我的问题是,如果窗口隐藏时处于最小化状态,我似乎无法将窗口带到前台。
例如,假设用户最小化应用程序,然后双击托盘图标。然后应用程序窗口将隐藏并从任务栏中消失。当用户再次双击托盘图标时,应用程序窗口应该出现,即它应该从最小化状态恢复并带到前台。
下面的代码应该做到这一点,但由于某种原因它没有:
private void TrayIcon_DoubleClick(object sender, EventArgs e)
{
if (this.Visibility == Visibility.Hidden)
{
this.Visibility = Visibility.Visible;
this.WindowState = WindowState.Normal;
this.Activate();
}
...
}
应用程序保持最小化并且不会进入前台。 Activate() 返回 true,随后对 TrayIcon_DoubleClick() 的调用表明状态确实设置为Normal。
My application has a tray icon which, when double-clicked, hides or shows the application window. My issue is that I can't seem to bring the window to the foreground if it was in a minimized state when it was hidden.
For instance, say the user minimizes the application and then double-clicks the tray icon. The application window is then hidden and disappears from the taskbar. When the user double-clicks the tray icon again, the application window should appear, i.e. it should be restored from the minimized state and brought to the foreground.
The code below ought to do just that, but for some reason it doesn't:
private void TrayIcon_DoubleClick(object sender, EventArgs e)
{
if (this.Visibility == Visibility.Hidden)
{
this.Visibility = Visibility.Visible;
this.WindowState = WindowState.Normal;
this.Activate();
}
...
}
The application stays minimized and isn't brought to the foreground. Activate() returns true and subsequent calls to TrayIcon_DoubleClick() indicate that the state is indeed set to Normal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 MSDN 论坛上交叉发布了我的问题 那里得到了答复。引用答案:
Window 上的一些属性更像是方法,从某种意义上说,它们会导致复杂的操作发生,需要在前一个操作完成后发生。实现这一目标的一种方法是使用 Dispatcher.BeginInvoke。如果您将代码更改为如下所示,它应该可以工作:
我尝试了这一点,它为我解决了问题。另外,我认为您也可以省略
this.Activate()
。I cross posted my question on the MSDN Forums and it got answered there. To quote the answer:
Some properties on Window that are more like methods, in the sense they cause complex actions to happen, need to happen after the previous action has already completed. One way to get that to happen is using
Dispatcher.BeginInvoke
. If you change your code to look like this, it should work:I tried this out and it fixed the problem for me. Also, I think you can leave out the
this.Activate()
as well.我找到了更好的方法。由于更改窗口的可见性和窗口状态时出现问题,我所做的是更改属性 ShowInTaskBar 而不是 Visibility。无论如何,带有
ShowInTaskBar = true
的最小化窗口就像一个隐藏窗口。I found a better way. As the problem happens when changing the visibility of the window and the window state what I do is changing the property ShowInTaskBar instead of Visibility. Anyway a minimized window with
ShowInTaskBar = true
is like a hidden window.从用户角度
单击最小化的图标
然后,这应该显示应用程序所有实例的列表。
右键单击此列表的成员
选择最大化。
请注意,右键单击最小化图标将弹出一个带有关闭选项的菜单。
要获得“最大化”选项,您需要右键单击单击该图标时出现的列表。
From the user perspective
Click the minimized icon
This should then show a list of all instances of the application.
right click a member of this list
select maximize.
Note right clicking the minimized icon will bring up a menu with the close option.
To get the Maximise option you need to right click the list that appears when you click the icon.