简体   繁体   English

使窗口从折叠可见后的所有其他窗口下的WPF窗口

[英]WPF Window under all other windows after making window visible from being collapsed

I am having some very frustrating issues with my WPF window. 我的WPF窗口出现一些令人沮丧的问题。 The design I was going for is: 我要设计的是:

When the user minimizes the window it will minimize to a system tray icon (hide the window and icon on start bar). 当用户最小化窗口时,它将最小化为系统任务栏图标(将窗口和图标隐藏在开始栏上)。 When the user right clicks on the icon, a context menu will show up with options and one of them will say open, which will open the app (show window again / unhide window agian). 当用户右键单击该图标时,将显示一个带有选项的上下文菜单,其中一个选项将说“打开”,这将打开该应用程序(再次显示窗口/取消隐藏窗口)。 The user could also double click the icon as well. 用户也可以双击图标。

Pretty simple right? 很简单吧?

I have no issues minimzing to the task bar, I simply do the following on the closing event of the window: 我没有任何问题最小化到任务栏,我只对窗口的关闭事件执行以下操作:

e.Cancel = true;
this.Visibility = Visibility.Collapsed;

However, I am having issues properly restoring the window. 但是,我在正确还原窗口时遇到了问题。 I simply do this on the context menu click or icon double click event: 我只需在上下文菜单中单击“ click”或“图标”双击事件即可:

this.Visibility = Visibility.Visible;
this.Activate();

The issue is that the window is once again on the start bar with its icon but it is behind every single window the user has open. 问题是该窗口再次位于带有其图标的开始栏上,但位于用户打开的每个窗口的后面。 I want it so when the user goes to open the window it will be the top most window. 我想要这样,以便当用户打开窗口时它将是最上面的窗口。 I do not always want it to be the topmost, just only when they want to make it visible again. 我并不总是希望它成为最高的,只是当他们想再次使它可见时。

I have tried many things like setting the show activate on the window to true, waiting a second after making it visible to then activate the window, activating the window multiple times (worked a few times but was maybe 1 out of 10 tries), etc.. 我尝试了很多事情,例如将show show on on window设置为true,使其可见后等待一秒钟,然后再激活该窗口,多次激活该窗口(工作了几次,但可能是10次尝试中的1次),等等。 ..

I don't think showing / hiding a window should be this annoying and I am not really sure what I am doing wrong. 我不认为显示/隐藏窗口会很烦人,而且我不确定自己做错了什么。

Any help is appreciated, thank you. 任何帮助表示赞赏,谢谢。

Only after posting this did I realize, the application minimizes first before hiding. 仅在发布此内容后我才意识到,应用程序在隐藏之前先最小化。 When I show the window, it was showing as minimized. 当我显示窗口时,它显示为最小化。

After knowing this issue I was able to fix the issue. 知道此问题后,我得以解决此问题。 This may help others who do decide to hide the window after minimizing. 这可以帮助确实决定最小化后隐藏窗口的其他人。

EDIT Here is the code I used to hide the window (this is called after the event fires for state changed [minimized]): 编辑这是我用来隐藏窗口的代码(在事件触发状态更改为[最小化]之后调用):

Application.Current.MainWindow.Visibility = Visibility.Collapsed;
Application.Current.MainWindow.WindowState = WindowState.Normal;

You will notice I set the window state back to normal after I hide it. 您会发现在隐藏窗口状态后,我将其恢复为正常状态。 Even though the window is hidden and not being rendered it will in memory restore the window location / size. 即使窗口是隐藏的并且不呈现,它也会在内存中恢复窗口的位置/大小。

Then when I want to see the window again I just do: 然后,当我想再次看到该窗口时,我只是这样做:

Application.Current.MainWindow.Visibility = Visibility.Visible;

Which will show the window just fine! 这将显示窗口就好了!

Hopes this helps someone out there! 希望这可以帮助某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM