Files
LanMountainDesktop/LanMountainDesktop.Launcher/Views/ErrorDebugWindow.axaml.cs

173 lines
5.2 KiB
C#
Raw Normal View History

2026-04-16 19:28:58 +08:00
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
namespace LanMountainDesktop.Launcher.Views;
/// <summary>
/// 错误调试窗口 - 开发人员专用调试设置
/// </summary>
public partial class ErrorDebugWindow : Window
{
private string? _selectedHostPath;
2026-04-17 15:16:01 +08:00
private bool _isInitialized = false;
2026-04-16 19:28:58 +08:00
/// <summary>
/// 是否启用了开发模式
/// </summary>
public bool IsDevModeEnabled { get; private set; }
/// <summary>
/// 选择的主程序路径
/// </summary>
public string? SelectedHostPath => _selectedHostPath;
public ErrorDebugWindow()
{
AvaloniaXamlLoader.Load(this);
2026-04-17 15:16:01 +08:00
// 延迟到窗口加载完成后再初始化组件
this.Loaded += OnWindowLoaded;
2026-04-16 19:28:58 +08:00
}
public ErrorDebugWindow(bool devModeEnabled, string? initialPath) : this()
{
IsDevModeEnabled = devModeEnabled;
_selectedHostPath = initialPath;
2026-04-17 15:16:01 +08:00
}
2026-04-16 19:28:58 +08:00
2026-04-17 15:16:01 +08:00
/// <summary>
/// 窗口加载完成事件
/// </summary>
private void OnWindowLoaded(object? sender, RoutedEventArgs e)
{
if (_isInitialized) return;
_isInitialized = true;
Console.WriteLine("[ErrorDebugWindow] Window loaded, initializing components...");
InitializeComponents();
// 设置初始值(在视觉树准备好后)
2026-04-16 19:28:58 +08:00
var devModeToggle = this.FindControl<ToggleSwitch>("DevModeToggle");
if (devModeToggle is not null)
{
2026-04-17 15:16:01 +08:00
devModeToggle.IsChecked = IsDevModeEnabled;
2026-04-16 19:28:58 +08:00
}
2026-04-17 15:16:01 +08:00
UpdatePathDisplay(_selectedHostPath);
2026-04-16 19:28:58 +08:00
}
private void InitializeComponents()
{
// 开发模式开关
var devModeToggle = this.FindControl<ToggleSwitch>("DevModeToggle");
if (devModeToggle is not null)
{
devModeToggle.IsCheckedChanged += (s, e) =>
{
IsDevModeEnabled = devModeToggle.IsChecked ?? false;
2026-04-17 15:16:01 +08:00
Console.WriteLine($"[ErrorDebugWindow] DevMode changed to: {IsDevModeEnabled}");
2026-04-16 19:28:58 +08:00
};
2026-04-17 15:16:01 +08:00
Console.WriteLine("[ErrorDebugWindow] DevModeToggle event bound");
}
else
{
Console.Error.WriteLine("[ErrorDebugWindow] Failed to find DevModeToggle!");
2026-04-16 19:28:58 +08:00
}
// 浏览按钮
var browseButton = this.FindControl<Button>("BrowseButton");
if (browseButton is not null)
{
browseButton.Click += OnBrowseClick;
2026-04-17 15:16:01 +08:00
Console.WriteLine("[ErrorDebugWindow] BrowseButton event bound");
}
else
{
Console.Error.WriteLine("[ErrorDebugWindow] Failed to find BrowseButton!");
2026-04-16 19:28:58 +08:00
}
// 确定按钮
var okButton = this.FindControl<Button>("OkButton");
if (okButton is not null)
{
okButton.Click += (s, e) => Close();
2026-04-17 15:16:01 +08:00
Console.WriteLine("[ErrorDebugWindow] OkButton event bound");
}
else
{
Console.Error.WriteLine("[ErrorDebugWindow] Failed to find OkButton!");
2026-04-16 19:28:58 +08:00
}
// 取消按钮
var cancelButton = this.FindControl<Button>("CancelButton");
if (cancelButton is not null)
{
cancelButton.Click += (s, e) =>
{
// 取消时恢复原始状态
IsDevModeEnabled = false;
_selectedHostPath = null;
2026-04-17 15:16:01 +08:00
Console.WriteLine("[ErrorDebugWindow] Cancel clicked, resetting state");
2026-04-16 19:28:58 +08:00
Close();
};
2026-04-17 15:16:01 +08:00
Console.WriteLine("[ErrorDebugWindow] CancelButton event bound");
2026-04-16 19:28:58 +08:00
}
2026-04-17 15:16:01 +08:00
else
{
Console.Error.WriteLine("[ErrorDebugWindow] Failed to find CancelButton!");
}
Console.WriteLine("[ErrorDebugWindow] Components initialization completed");
2026-04-16 19:28:58 +08:00
}
/// <summary>
/// 浏览按钮点击
/// </summary>
private async void OnBrowseClick(object? sender, RoutedEventArgs e)
{
var storageProvider = StorageProvider;
if (storageProvider is null) return;
var options = new FilePickerOpenOptions
{
Title = "选择阑山桌面主程序",
AllowMultiple = false,
FileTypeFilter = new[]
{
new FilePickerFileType("可执行文件")
{
Patterns = OperatingSystem.IsWindows()
? new[] { "*.exe" }
: new[] { "*" }
}
}
};
var result = await storageProvider.OpenFilePickerAsync(options);
if (result.Count > 0)
{
_selectedHostPath = result[0].Path.LocalPath;
2026-04-17 15:16:01 +08:00
Console.WriteLine($"[ErrorDebugWindow] Selected host path: {_selectedHostPath}");
2026-04-16 19:28:58 +08:00
UpdatePathDisplay(_selectedHostPath);
}
}
/// <summary>
/// 更新路径显示
/// </summary>
private void UpdatePathDisplay(string? path)
{
var pathTextBlock = this.FindControl<TextBlock>("PathTextBlock");
if (pathTextBlock is not null)
{
pathTextBlock.Text = string.IsNullOrEmpty(path) ? "未选择" : path;
}
2026-04-17 15:16:01 +08:00
else
{
Console.Error.WriteLine("[ErrorDebugWindow] Failed to find PathTextBlock!");
}
2026-04-16 19:28:58 +08:00
}
}