|
|
|
|
using System;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Controls.Primitives;
|
|
|
|
|
using Avalonia.Input;
|
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
|
using Avalonia.ReactiveUI;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using MyAvaloniaApp.ViewModels;
|
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using System.Reactive.Disposables;
|
|
|
|
|
|
|
|
|
|
namespace MyAvaloniaApp;
|
|
|
|
|
|
|
|
|
|
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>, IActivatableView
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<MainWindow>? _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 无参构造函数,用于 XAML 设计器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
SetupWindowControls();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数,接受依赖注入的 ViewModel
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="viewModel">主窗口的 ViewModel</param>
|
|
|
|
|
/// <param name="logger">日志记录器</param>
|
|
|
|
|
public MainWindow(MainWindowViewModel viewModel, ILogger<MainWindow>? logger = null)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(viewModel);
|
|
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
ViewModel = viewModel;
|
|
|
|
|
SetupWindowControls();
|
|
|
|
|
|
|
|
|
|
_logger?.LogInformation("MainWindow 已创建,ViewModel 已设置");
|
|
|
|
|
|
|
|
|
|
// 使用 WhenActivated 管理订阅
|
|
|
|
|
this.WhenActivated(disposables =>
|
|
|
|
|
{
|
|
|
|
|
// 可以在这里添加窗口级别的订阅,如果需要的话
|
|
|
|
|
// 例如:this.WhenAnyValue(x => x.ViewModel.Title)
|
|
|
|
|
// .Subscribe(title => ...)
|
|
|
|
|
// .DisposeWith(disposables);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeComponent()
|
|
|
|
|
{
|
|
|
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置窗口控制按钮事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void SetupWindowControls()
|
|
|
|
|
{
|
|
|
|
|
// 设置标题栏拖动功能
|
|
|
|
|
var titleBarGrid = this.FindControl<Grid>("TitleBarGrid");
|
|
|
|
|
if (titleBarGrid != null)
|
|
|
|
|
{
|
|
|
|
|
titleBarGrid.PointerPressed += OnTitleBarPointerPressed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 最小化按钮
|
|
|
|
|
var minimizeButton = this.FindControl<Button>("MinimizeButton");
|
|
|
|
|
if (minimizeButton != null)
|
|
|
|
|
{
|
|
|
|
|
minimizeButton.Click += (sender, e) => WindowState = WindowState.Minimized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 最大化/还原按钮
|
|
|
|
|
var maximizeButton = this.FindControl<Button>("MaximizeButton");
|
|
|
|
|
if (maximizeButton != null)
|
|
|
|
|
{
|
|
|
|
|
maximizeButton.Click += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (WindowState == WindowState.Maximized)
|
|
|
|
|
{
|
|
|
|
|
WindowState = WindowState.Normal;
|
|
|
|
|
ToolTip.SetTip(maximizeButton, "最大化");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WindowState = WindowState.Maximized;
|
|
|
|
|
ToolTip.SetTip(maximizeButton, "还原");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭按钮
|
|
|
|
|
var closeButton = this.FindControl<Button>("CloseButton");
|
|
|
|
|
if (closeButton != null)
|
|
|
|
|
{
|
|
|
|
|
closeButton.Click += (sender, e) => Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理标题栏鼠标按下事件,实现窗口拖动
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnTitleBarPointerPressed(object? sender, PointerPressedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
|
|
|
|
{
|
|
|
|
|
BeginMoveDrag(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|