From 328455e7ed489a5862bd2a700fb24ca142a5c60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E6=99=BA=E7=BA=AF?= <于智纯@LODESTAR> Date: Tue, 6 Jan 2026 16:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=AA=97=E4=BD=93?= =?UTF-8?q?=E6=A0=87=E9=A2=98=E6=A0=8F=E5=8F=8A=E5=B0=BA=E5=AF=B8=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=89=8B=E6=9F=84=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MVVMExample/Helper.cs | 54 ++++++++++++++++++++++++++ MVVMExample/Themes/DefaultStyles.axaml | 16 ++++++++ MVVMExample/Views/MainWindow.axaml | 40 +++++++++++++++---- MVVMExample/Views/MainWindow.axaml.cs | 41 ++++++++++++++++++- 4 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 MVVMExample/Helper.cs create mode 100644 MVVMExample/Themes/DefaultStyles.axaml diff --git a/MVVMExample/Helper.cs b/MVVMExample/Helper.cs new file mode 100644 index 0000000..8a01381 --- /dev/null +++ b/MVVMExample/Helper.cs @@ -0,0 +1,54 @@ +using Avalonia.Controls; +using Avalonia.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVVMExample +{ + public static partial class Helper + { + /// + /// 绑定自定义窗口标题栏与尺寸调整句柄 + /// + /// 要进行绑定的窗口 + /// 窗口位置拖动句柄 + /// 窗口尺寸调整句柄 + /// 最大化按钮 + /// 最小化按钮 + /// 关闭按钮 + public static void BindWindowEdge(this Window window, InputElement? handle = null, InputElement? slider = null, Button? maxBut = null, Button? minBut = null, Button? closeBut = null) + { + // 隐藏窗口默认标题栏与尺寸调整句柄 + window.SystemDecorations = SystemDecorations.None; + + if (handle != null) + handle.PointerPressed += (s, e) => + { + if (e.GetCurrentPoint(window).Properties.IsLeftButtonPressed) window.BeginMoveDrag(e); // 关键:开始窗口拖拽 + }; + if (slider != null) + slider.PointerPressed += (s, e) => + { + if (e.GetCurrentPoint(window).Properties.IsLeftButtonPressed) window.BeginResizeDrag(WindowEdge.SouthEast, e); + }; + if (maxBut != null) + maxBut.Click += (s, e) => + { + window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; + }; + if (minBut != null) + minBut.Click += (s, e) => + { + window.WindowState = WindowState.Minimized; + }; + if (closeBut != null) + closeBut.Click += (s, e) => + { + window.Close(); + }; + } + } +} diff --git a/MVVMExample/Themes/DefaultStyles.axaml b/MVVMExample/Themes/DefaultStyles.axaml new file mode 100644 index 0000000..a723879 --- /dev/null +++ b/MVVMExample/Themes/DefaultStyles.axaml @@ -0,0 +1,16 @@ + + + + + + + + + + + diff --git a/MVVMExample/Views/MainWindow.axaml b/MVVMExample/Views/MainWindow.axaml index b955207..e2074de 100644 --- a/MVVMExample/Views/MainWindow.axaml +++ b/MVVMExample/Views/MainWindow.axaml @@ -1,4 +1,4 @@ - - + Title="MVVMExample" + CanResize="True"> + + + - - - - + + + + + + + + + + + +