WPF: Window stays minimized even when setting WindowState explicitly

前端 未结 3 1921
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 18:28

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

3条回答
  •  失恋的感觉
    2021-01-17 18:32

    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.

提交回复
热议问题