title: C# WPF window not positioned on the center when it's being shown from hidden state on the application startup
body: <p>I've built a C# WPF application where:</p>
  
  <ul>
  <li>It has a system tray icon.</li>
  <li>It can accept a command line argument <code>--minimize-to-tray</code> 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".</li>
  <li>On app startup, window position should be at the center of the screen.</li>
  </ul>
  
  <p>My window is only one, that is, MainWindow, where on its XAML I set</p>
  
  <pre class="lang-cs prettyprint-override"><code>WindowStartupLocation="CenterScreen"
  </code></pre>
  
  <p>to make the window positioned at the center of the screen.</p>
  
  <p>All is great. At normal condition without the argument, the application is centered.</p>
  
  <p>...However, not for this one: when I run the app with argument <code>--minimize-to-tray</code> and double-click the sys tray icon to reveal the window, it is not centered.</p>
  
  <p><br>
  Here is my code to accept arguments and its associated actions:</p>
  
  <pre class="lang-cs prettyprint-override"><code>...
  
  /// &lt;summary&gt;
  /// MainWindow constructor
  /// &lt;summary&gt;
  public MainWindow()
  {
      InitializeComponent();
  
      InitializeAppArguments();
  
      ...
  }
  
  /// &lt;summary&gt;
  /// Initialize actions that are associated with application's arguments.
  /// &lt;/summary&gt;
  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 &gt;= 2)
      {
          if (args[1] == "--minimize-to-tray")
          {
              this.WindowState = WindowState.Minimized;
              this.Hide();
  
              this.StateChanged += MainWindow_StateChanged;
          }
      }
  }
  
  ...
  
  /// &lt;summary&gt;
  /// Hide app's window from taskbar whenever user minimize the app window.
  /// &lt;/summary&gt;
  /// &lt;param name="sender"&gt;&lt;/param&gt;
  /// &lt;param name="e"&gt;&lt;/param&gt;
  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();
      }
  }
  </code></pre>
  
  <p><br>
  And event handler on system tray icon's double click or click on "Open" context menu of the icon:</p>
  
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Open/show application when user click on "Open" context menu on application's system tray icon.
  /// &lt;/summary&gt;
  /// &lt;param name="sender"&gt;&lt;/param&gt;
  /// &lt;param name="e"&gt;&lt;/param&gt;
  public void MenuOpen_Click__SysTrayIcon_DoubleClick(object sender, EventArgs e)
  {
      this.Show();
      this.Focus();
  
      this.WindowState = WindowState.Normal;
  }
  </code></pre>
  
  <p><br>
  So, why is the window not centered when it's being shown from hidden on the application startup? Am I doing something wrong here?</p>
  
  <p>Any help would be appreciated.</p>