浏览代码

Fix state not saved on system reboot

by switching from App.Exit to Window.Closing event
Daniel Chalmers 3 年之前
父节点
当前提交
9817fd8e64
共有 4 个文件被更改,包括 17 次插入11 次删除
  1. 0 1
      DesktopClock/App.xaml
  2. 0 8
      DesktopClock/App.xaml.cs
  3. 2 2
      DesktopClock/MainWindow.xaml
  4. 15 0
      DesktopClock/MainWindow.xaml.cs

+ 0 - 1
DesktopClock/App.xaml

@@ -1,7 +1,6 @@
 <Application x:Class="DesktopClock.App"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             Exit="Application_Exit"
              StartupUri="MainWindow.xaml">
 	<Application.Resources />
 </Application>

+ 0 - 8
DesktopClock/App.xaml.cs

@@ -1,6 +1,5 @@
 using System.Collections.Generic;
 using System.Windows;
-using DesktopClock.Properties;
 
 namespace DesktopClock;
 
@@ -23,11 +22,4 @@ public partial class App : Application
         new Theme("Yellow", "#FFD600", "#FFFF8D"),
         new Theme("Orange", "#FF6D00", "#FFD180"),
     };
-
-    private void Application_Exit(object sender, ExitEventArgs e)
-    {
-        Settings.Default.SaveIfNotModifiedExternally();
-
-        SettingsHelper.SetRunOnStartup(Settings.Default.RunOnStartup);
-    }
 }

+ 2 - 2
DesktopClock/MainWindow.xaml

@@ -5,7 +5,6 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:DesktopClock"
         xmlns:p="clr-namespace:DesktopClock.Properties"
-        xmlns:wp="clr-namespace:WpfWindowPlacement;assembly=WpfWindowPlacement"
         mc:Ignorable="d"
         Title="DesktopClock"
         AllowsTransparency="True"
@@ -20,7 +19,8 @@
         MouseDown="Window_MouseDown"
         MouseDoubleClick="Window_MouseDoubleClick"
         MouseWheel="Window_MouseWheel"
-        wp:WindowPlacementProperties.Placement="{Binding Placement, Source={x:Static p:Settings.Default}}">
+		SourceInitialized="Window_SourceInitialized"
+		Closing="Window_Closing">
 	<Window.DataContext>
 		<local:MainViewModel />
 	</Window.DataContext>

+ 15 - 0
DesktopClock/MainWindow.xaml.cs

@@ -3,6 +3,7 @@ using System.Diagnostics;
 using System.Windows;
 using System.Windows.Input;
 using DesktopClock.Properties;
+using WpfWindowPlacement;
 
 namespace DesktopClock;
 
@@ -72,4 +73,18 @@ public partial class MainWindow : Window
     {
         Close();
     }
+
+    private void Window_SourceInitialized(object sender, EventArgs e)
+    {
+        WindowPlacementFunctions.SetPlacement(this, Settings.Default.Placement);
+    }
+
+    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
+    {
+        Settings.Default.Placement = WindowPlacementFunctions.GetPlacement(this);
+
+        Settings.Default.SaveIfNotModifiedExternally();
+
+        SettingsHelper.SetRunOnStartup(Settings.Default.RunOnStartup);
+    }
 }