如何知道Last Window-Startup-Location何时关闭? WPF应用程序

时间:2019-03-25 06:06:27

标签: c# .net wpf

我正在使用WPF应用程序,该应用程序会在特定时间间隔后自动重新启动(为此,我正在使用DispatcherTimer)。问题是如何知道WPF窗口的最后一个Startup_Location。因为我需要在重新启动时设置该位置。

((我已经在XAML代码中设置了默认的Startup_Location“ CenterOwner”

WindowStartupLocation="CenterOwner"

但是如果用户更改了该位置,那么我需要知道WPF重新启动时要设置的位置)

其次,上次如何知道WPF窗口已最小化,因为它在重启时也需要设置 谢谢

1 个答案:

答案 0 :(得分:1)

正如其他人已经指出的那样,您将必须存储窗口的属性以在开始之间保留它们。为了设置Window的位置,您必须将WindowStartupLocation设置为WindowStartupLocation.Manual。您必须从 App.xaml.cs 手动启动主窗口。

为了更改WPF启动应用程序的方式,您必须修改 App.xaml 并将活动属性StartupUri更改为事件处理程序属性Startup,然后为它分配一个处理程序方法(在本例中为Run):

<Application x:Class="WpfTestRange.Main.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Run">
</Application>

然后在 App.xaml.cs 中实现处理程序,并手动启动主窗口:

private void Run(object sender, StartupEventArgs e)
{
  this.MainWindow = new MainWindow();
  this.MainWindow.Closing += SaveWindowAttributes;
  RestoreWindow();
  this.MainWindow.Show();
}

protected void RestoreWindow()
{
  this.MainWindow.WindowStartupLocation = WindowStartupLocation.Manual;
  var reader = new AppSettingsReader();

  string windowStateValue;
  try
  {
    windowStateValue = reader.GetValue("WindowState", typeof(string)) as string;
  }
  catch (InvalidOperationException)
  {
    // There are no previously persisted values (first launch)
    return;
  }

  WindowState windowState;
  if (!string.IsNullOrWhiteSpace(windowStateValue) && Enum.TryParse(windowStateValue, out windowState))
  {
    this.MainWindow.WindowState = windowState;
  }

  string windowTopValue = reader.GetValue("WindowPositionTop", typeof(string)) as string;
  string windowLeftValue = reader.GetValue("WindowPositionLeft", typeof(string)) as string;

  double windowPositionTop;
  double windowPositionLeft;
  if (!string.IsNullOrWhiteSpace(windowStateValue) 
      && double.TryParse(windowTopValue, out windowPositionTop) 
      && double.TryParse(windowLeftValue, out windowPositionLeft))
  {
    this.MainWindow.Top = windowPositionTop;
    this.MainWindow.Left = windowPositionLeft;
  }

}

private void SaveWindowAttributes(object sender, CancelEventArgs e)
{
  string sectionName = "appSettings";
  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

  string currentWindowState = this.MainWindow.WindowState.ToString();
  if (config.AppSettings.Settings["WindowState"] == null)
  {
    config.AppSettings.Settings.Add("WindowState", currentWindowState);
  }
  else
  {
    config.AppSettings.Settings["WindowState"].Value = currentWindowState;
  }

  string currentWindowPositionTop = this.MainWindow.Top.ToString("G");
  if (config.AppSettings.Settings["WindowPositionTop"] == null)
  {
    config.AppSettings.Settings.Add("WindowPositionTop", currentWindowPositionTop);
  }
  else
  {
    config.AppSettings.Settings["WindowPositionTop"].Value = currentWindowPositionTop;
  }

  string currentWindowPositionLeft = this.MainWindow.Left.ToString("G");
  if (config.AppSettings.Settings["WindowPositionLeft"] == null)
  {
    config.AppSettings.Settings.Add("WindowPositionLeft", currentWindowPositionLeft);
  }
  else
  {
    config.AppSettings.Settings["WindowPositionLeft"].Value = currentWindowPositionLeft;
  }
  config.Save(ConfigurationSaveMode.Full);
  ConfigurationManager.RefreshSection(sectionName);
}