简体   繁体   English

在应用程序启动时从隐藏状态显示C#WPF窗口时,它不在中央

[英]C# WPF window not positioned on the center when it's being shown from hidden state on the application startup

I've built a C# WPF application where: 我建立了一个C#WPF应用程序,其中:

  • It has a system tray icon. 它具有系统托盘图标。
  • It can accept a command line argument --minimize-to-tray to make the window hidden from the taskbar at the application startup, and to open it we have to double click at the sys tray icon or right click at the icon and select "Open". 它可以接受命令行参数--minimize-to-tray ,以在应用程序启动时将窗口从任务栏隐藏起来,要打开该窗口,我们必须双击sys tray图标或右键单击该图标,然后选择“打开”。
  • On app startup, window position should be at the center of the screen. 在应用启动时,窗口位置应位于屏幕中央。

My window is only one, that is, MainWindow, where on its XAML I set 我的窗口只有一个,即MainWindow,我在其中设置了XAML

WindowStartupLocation="CenterScreen"

to make the window positioned at the center of the screen. 使窗口位于屏幕中央。

All is great. 一切都很好。 At normal condition without the argument, the application is centered. 在没有参数的正常情况下,应用程序居中。

...However, not for this one: when I run the app with argument --minimize-to-tray and double-click the sys tray icon to reveal the window, it is not centered. ...但是,并非如此:当我使用参数--minimize-to-tray运行应用程序并双击sys托盘图标以显示该窗口时,它并未居中。


Here is my code to accept arguments and its associated actions: 这是我接受参数及其相关动作的代码:

...

/// <summary>
/// MainWindow constructor
/// <summary>
public MainWindow()
{
    InitializeComponent();

    InitializeAppArguments();

    ...
}

/// <summary>
/// Initialize actions that are associated with application's arguments.
/// </summary>
public void InitializeAppArguments()
{
    string[] args = Environment.GetCommandLineArgs();

    // Minimize window on app startup to tray 
    // if user put first argument "--minimize-to-tray" on the app
    //
    if (args.Length >= 2)
    {
        if (args[1] == "--minimize-to-tray")
        {
            this.WindowState = WindowState.Minimized;
            this.Hide();

            this.StateChanged += MainWindow_StateChanged;
        }
    }
}

...

/// <summary>
/// Hide app's window from taskbar whenever user minimize the app window.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void MainWindow_StateChanged(object sender, EventArgs e)
{
    // When app's window is minimized, hide it from taskbar
    //
    if (this.WindowState == WindowState.Minimized)
    {
        this.Hide();
    }
}


And event handler on system tray icon's double click or click on "Open" context menu of the icon: 然后双击系统任务栏图标上的事件处理程序,或单击该图标的“打开”上下文菜单:

/// <summary>
/// Open/show application when user click on "Open" context menu on application's system tray icon.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void MenuOpen_Click__SysTrayIcon_DoubleClick(object sender, EventArgs e)
{
    this.Show();
    this.Focus();

    this.WindowState = WindowState.Normal;
}


So, why is the window not centered when it's being shown from hidden on the application startup? 那么,为什么在应用程序启动时从隐藏状态显示窗口时窗口没有居中? Am I doing something wrong here? 我在这里做错什么了吗?

Any help would be appreciated. 任何帮助,将不胜感激。

According to this solution : 根据此解决方案

It is necessary to set WindowState to Normal before setting window location. 在设置窗口位置之前,必须将WindowState设置为Normal。


Apparently, it also works to prevent the window appears uncentered. 显然,它还可以防止窗口居中显示。 My mistake is that before I hide the window, I set the WindowState to Minimized, which makes the window uncentered. 我的错误是,在隐藏窗口之前,我将WindowState设置为Minimized,这使窗口不居中。

So, to solve that, I change the line this.WindowState = WindowState.Minimized; 因此,为解决此问题,我将代码行更改为this.WindowState = WindowState.Minimized; to this.WindowState = WindowState.Normal; this.WindowState = WindowState.Normal; .

public void InitializeAppArguments()
{
    string[] args = Environment.GetCommandLineArgs();

    // Minimize window on app startup to tray 
    // if user put first argument "--minimize-to-tray" on the app
    //
    if (args.Length >= 2)
    {
        if (args[1] == "--minimize-to-tray")
        {
            this.WindowState = WindowState.Normal; // Fixed the problem
            this.Hide();

            this.StateChanged += MainWindow_StateChanged;
        }
    }
}

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

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