c# - 如何知道 Last Window-Startup-Location 刚刚关闭的时间?WPF 应用程序
问题描述
我正在开发一个 WPF 应用程序,该应用程序会在特定时间间隔后自动重启(我为此目的使用 DispatcherTimer)。问题是如何知道 WPF 窗口的最后一个 Startup_Location .. 因为我需要在重新启动时设置该位置..
(我在 XAML 代码中设置了 Default Startup_Location “CenterOwner”,例如
WindowStartupLocation="CenterOwner"
但如果用户更改该位置,那么我需要知道 WPF 重新启动时要设置的位置)
其次,如何知道 WPF 窗口上次最小化,因为这也需要在重新启动时设置 谢谢
解决方案
正如其他人已经指出的那样,您必须存储窗口的属性以在开始之间保持它们。为了设置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);
}
推荐阅读
- c++ - C++ SDL 项目的 Makefile
- python - 如何在 Keras 中控制微分链式法则
- c# - 单元测试嵌套方法
- c++ - 使用 std::filesystem,错误显示无法将 int 转换为 _Valty
- windows-server-2012-r2 - robots.txt 文件或控制它的配置是否有备用位置?
- oracle - 在 oracle apex 中使用圆环图。我在圆环图的分区上创建了链接,单击时将打开一个模式窗口到另一个页面
- javascript - 获取数组值的总和时如何修复 [Object object] 响应?
- javascript - Gulp 自定义函数返回并行不会做任何事情
- java - CompassLayer 未出现在 WorldWind 中
- reactjs - Webpack-dev-server 和 react-i18next XHR 后端