浏览代码

Merge branch 'master' into fixes/11119-carousel-control-items

Max Katz 2 年之前
父节点
当前提交
55e0a31c13

+ 2 - 8
samples/Sandbox/MainWindow.axaml.cs

@@ -6,17 +6,11 @@ using Avalonia.Win32.WinRT.Composition;
 
 namespace Sandbox
 {
-    public class MainWindow : Window
+    public partial class MainWindow : Window
     {
         public MainWindow()
         {
-            this.InitializeComponent();
-            this.AttachDevTools();
-        }
-
-        private void InitializeComponent()
-        {
-            AvaloniaXamlLoader.Load(this);
+            InitializeComponent();
         }
     }
 }

+ 2 - 0
samples/Sandbox/Sandbox.csproj

@@ -4,6 +4,7 @@
     <OutputType>WinExe</OutputType>
     <TargetFramework>net6.0</TargetFramework>
     <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
+    <IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators>
   </PropertyGroup>
 
   <ItemGroup>
@@ -17,4 +18,5 @@
   <Import Project="..\..\build\SampleApp.props" />
   <Import Project="..\..\build\ReferenceCoreLibraries.props" />
   <Import Project="..\..\build\BuildTargets.targets" />
+  <Import Project="..\..\build\SourceGenerators.props" />
 </Project>

+ 19 - 4
src/Avalonia.Controls/WindowBase.cs

@@ -83,6 +83,19 @@ namespace Avalonia.Controls
         /// <summary>
         /// Occurs when the window is resized.
         /// </summary>
+        /// <remarks>
+        /// Although this event is similar to the <see cref="Control.SizeChanged"/> event, they are
+        /// conceptually different:
+        /// 
+        /// - <see cref="Resized"/> is a window-level event, fired when a resize notification arrives
+        ///   from the platform windowing subsystem. The event args contain details of the source of
+        ///   the resize event in the <see cref="WindowResizedEventArgs.Reason"/> property. This
+        ///   event is raised before layout has been run on the window's content.
+        /// - <see cref="Control.SizeChanged"/> is a layout-level event, fired when a layout pass
+        ///   completes on a control. <see cref="Control.SizeChanged"/> is present on all controls
+        ///   and is fired when the control's size changes for any reason, including a
+        ///   <see cref="Resized"/> event in the case of a Window.
+        /// </remarks>
         public event EventHandler<WindowResizedEventArgs>? Resized;
 
         public new IWindowBaseImpl? PlatformImpl => (IWindowBaseImpl?) base.PlatformImpl;
@@ -237,14 +250,16 @@ namespace Avalonia.Controls
         {
             FrameSize = PlatformImpl?.FrameSize;
 
-            if (ClientSize != clientSize)
+            var clientSizeChanged = ClientSize != clientSize;
+
+            ClientSize = clientSize;
+            OnResized(new WindowResizedEventArgs(clientSize, reason));
+
+            if (clientSizeChanged)
             {
-                ClientSize = clientSize;
                 LayoutManager.ExecuteLayoutPass();
                 Renderer.Resized(clientSize);
             }
-
-            OnResized(new WindowResizedEventArgs(clientSize, reason));
         }
 
         /// <summary>

+ 18 - 10
src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml.cs

@@ -39,16 +39,6 @@ namespace Avalonia.Diagnostics.Views
             AdornerLayer.SetIsClipEnabled(_adorner, false);
         }
 
-        protected override void OnDataContextChanged(EventArgs e)
-        {
-            base.OnDataContextChanged(e);
-
-            ((TreePageViewModel)DataContext!).ClipboardCopyRequested += (sender, s) =>
-            {
-                TopLevel.GetTopLevel(this)?.Clipboard?.SetTextAsync(s);
-            };
-        }
-
         protected void AddAdorner(object? sender, PointerEventArgs e)
         {
             var node = (TreeNode?)((Control)sender!).DataContext;
@@ -108,9 +98,27 @@ namespace Avalonia.Diagnostics.Views
             _currentLayer = null;
         }
 
+        protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
+        {
+            base.OnPropertyChanged(change);
+
+            if (change.Property == DataContextProperty)
+            {
+                if (change.GetOldValue<object?>() is TreePageViewModel oldViewModel)
+                    oldViewModel.ClipboardCopyRequested -= OnClipboardCopyRequested;
+                if (change.GetNewValue<object?>() is TreePageViewModel newViewModel)
+                    newViewModel.ClipboardCopyRequested += OnClipboardCopyRequested;
+            }
+        }
+
         private void InitializeComponent()
         {
             AvaloniaXamlLoader.Load(this);
         }
+
+        private void OnClipboardCopyRequested(object? sender, string e)
+        {
+            TopLevel.GetTopLevel(this)?.Clipboard?.SetTextAsync(e);
+        }
     }
 }