WPF: Window stays minimized even when setting WindowState explicitly
Asked Answered
N

3

9

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.

Nole answered 6/3, 2010 at 6:26 Comment(0)
N
18

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:

if (this.Visibility == Visibility.Hidden)
{
    this.Visibility = Visibility.Visible;
    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        new Action(delegate()
        {
            this.WindowState = WindowState.Normal;
            this.Activate();
        })
    );
}

I tried this out and it fixed the problem for me. Also, I think you can leave out the this.Activate() as well.

Nole answered 6/3, 2010 at 16:6 Comment(2)
Ok, that fixes the problem but that sucks don't you think? I mean, how are you supposed to know when you can use a property or not? I find this very strange ...Toshikotoss
Worked for me too, thanks. I find it quite frustrating that I should have to wrap a simple property change into this verbose mess. I expect the framework to handle this complexity internally.Klondike
T
1

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.

Toshikotoss answered 20/9, 2011 at 14:5 Comment(0)
M
0

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.

Mandatory answered 25/7, 2015 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.