【问题标题】:How to know Last Window-Startup-Location When it was just closed ? WPF Application [duplicate]如何知道 Last Window-Startup-Location 刚刚关闭的时间? WPF应用程序[重复]
【发布时间】:2019-03-25 06:06:26
【问题描述】:

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

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

WindowStartupLocation="CenterOwner"

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

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

【问题讨论】:

  • 您可以在窗口关闭之前将所需的信息保存在一个xml文件中,并在再次加载窗口之前从xml中读取信息。
  • 感谢@MathivananKP 的回复。如何在 xmal 中保存该信息并在重新加载时应用?我是 WPF 的新手..
  • 您可以将状态保存在用户配置文件中。 Here 是怎么回事。

标签: c# .net wpf


【解决方案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中实现handler,手动启动主窗口:

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);
}

【讨论】:

  • @BilalTahir 我编辑了答案以显示此步骤。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 2018-04-22
  • 1970-01-01
  • 2016-04-20
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多