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(); }; } } }