locked
Window stays minimized even when setting WindowState explicitly RRS feed

  • Question

  • 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 .
    Saturday, March 6, 2010 6:22 AM

Answers

  • Hi Soren,

    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.

    Andre
    Saturday, March 6, 2010 9:03 AM

All replies

  • Hi Soren,

    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.

    Andre
    Saturday, March 6, 2010 9:03 AM
  • Hi Soren,

    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. 
    That did it! I had a hunch that some of the actions had to be performed later, but I had already tried changing the window state and activating the window in the IsVisible() handler as well as the StatusChanged() handler, but neither worked. Turns out there was yet another way of deferring the actions :)

    Thanks!
    Saturday, March 6, 2010 3:57 PM