首页 > 解决方案 > 如何知道 Last Window-Startup-Location 刚刚关闭的时间?WPF 应用程序

问题描述

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

(我在 XAML 代码中设置了 Default Startup_Location “CenterOwner”,例如

WindowStartupLocation="CenterOwner"

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

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

标签: c#.netwpf

解决方案


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

为了更改 WPF 启动应用程序的方式,您必须修改App.xaml并将 active 属性更改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);
}

推荐阅读