Browse Source

Fix some window placement issues

Daniel Chalmers 1 năm trước cách đây
mục cha
commit
a632b4299a

+ 1 - 1
DesktopClock/DesktopClock.csproj

@@ -22,7 +22,7 @@
     <PackageReference Include="Humanizer.Core" Version="2.14.1" />
     <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
     <PackageReference Include="PropertyChanged.Fody" Version="4.1.0" PrivateAssets="All" />
-    <PackageReference Include="WpfWindowPlacement" Version="4.0.2" />
+    <PackageReference Include="WpfWindowPlacement" Version="6.0.0" />
   </ItemGroup>
 
   <ItemGroup>

+ 3 - 3
DesktopClock/MainWindow.xaml

@@ -6,6 +6,7 @@
         xmlns:local="clr-namespace:DesktopClock"
         xmlns:p="clr-namespace:DesktopClock.Properties"
         xmlns:tb="http://www.hardcodet.net/taskbar"
+        xmlns:wp="clr-namespace:WpfWindowPlacement;assembly=WpfWindowPlacement"
         d:DataContext="{d:DesignInstance Type=local:MainWindow}"
         mc:Ignorable="d"
         Title="DesktopClock"
@@ -21,10 +22,9 @@
         MouseDown="Window_MouseDown"
         MouseDoubleClick="Window_MouseDoubleClick"
         MouseWheel="Window_MouseWheel"
-        SourceInitialized="Window_SourceInitialized"
-        Deactivated="Window_Deactivated"
+		ContentRendered="Window_ContentRendered"
         Closed="Window_Closed"
-        SizeChanged="Window_SizeChanged">
+        wp:WindowPlacementProperties.Placement="{Binding Placement, Source={x:Static p:Settings.Default}, Mode=TwoWay}">
 	<Window.Resources>
 		<ContextMenu x:Key="MainContextMenu" x:Shared="False">
 			<MenuItem Command="{Binding CopyToClipboardCommand}" Header="_Copy" />

+ 7 - 9
DesktopClock/MainWindow.xaml.cs

@@ -11,7 +11,6 @@ using CommunityToolkit.Mvvm.Input;
 using DesktopClock.Properties;
 using H.NotifyIcon;
 using Humanizer;
-using WpfWindowPlacement;
 
 namespace DesktopClock;
 
@@ -21,6 +20,7 @@ namespace DesktopClock;
 [ObservableObject]
 public partial class MainWindow : Window
 {
+    private bool _hasInitiallyChangedSize;
     private readonly SystemClockTimer _systemClockTimer;
     private TaskbarIcon _trayIcon;
     private TimeZoneInfo _timeZone;
@@ -269,14 +269,9 @@ public partial class MainWindow : Window
         }
     }
 
-    private void Window_SourceInitialized(object sender, EventArgs e)
+    private void Window_ContentRendered(object sender, EventArgs e)
     {
-        WindowPlacementFunctions.SetPlacement(this, Settings.Default.Placement);
-    }
-
-    private void Window_Deactivated(object sender, EventArgs e)
-    {
-        Settings.Default.Placement = WindowPlacementFunctions.GetPlacement(this);
+        SizeChanged += Window_SizeChanged;
     }
 
     private void Window_Closed(object sender, EventArgs e)
@@ -290,10 +285,13 @@ public partial class MainWindow : Window
 
     private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
     {
-        if (e.WidthChanged && Settings.Default.RightAligned)
+        if (_hasInitiallyChangedSize && e.WidthChanged && Settings.Default.RightAligned)
         {
             var previousRight = Left + e.PreviousSize.Width;
             Left = previousRight - ActualWidth;
         }
+
+        // Use this to ignore the change when the window is loaded at the beginning.
+        _hasInitiallyChangedSize = true;
     }
 }