Files
ShrlAlgoToolkit/MelskinTest/MainWindow.xaml.cs
2026-03-01 10:42:42 +08:00

591 lines
18 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Melskin.Appearance;
using Melskin.Controls;
namespace MelskinTest;
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
[ObservableObject]
public partial class MainWindow
{
#region
public ObservableCollection<TreeNodeItem> HierarchicalData { get; set; }
public ObservableCollection<PersonItem> DataItems { get; set; }
public ObservableCollection<PersonItem> SelectedDataItems { get; set; }
public ObservableCollection<TreeNodeItem> Nodes { get; set; }
public ObservableCollection<object> SelectedNodes { get; set; }
public ObservableCollection<TreeNodeItem> StaffList { get; set; } = [];
public ObservableCollection<Area> SelectedObservableAreas { get; set; } = [];
#endregion
#region
[ObservableProperty]
public partial TreeNodeItem SelectedItem { get; set; }
[ObservableProperty]
public partial Area AutoArea { get; set; }
[ObservableProperty]
public partial Area SearchArea { get; set; }
partial void OnSearchAreaChanged(Area value)
{
MessageBox.Show($"搜索选择了:{value?.Name}");
}
private Area? _area;
public Area? Area
{
get => _area;
set
{
Debug.WriteLine($"VM.Area setter: incoming = {value?.Name ?? "null"}");
//if (value == null)
//{
// // 打印调用栈,帮助定位是谁把它设为 null只在收到 null 时打印以减少噪音)
// var st = new System.Diagnostics.StackTrace(true);
// Debug.WriteLine("StackTrace for setting Area = null:");
// Debug.WriteLine(st.ToString());
//}
_area = value;
OnPropertyChanged();
}
}
[ObservableProperty]
public partial List<Area> SelectedListAreas { get; set; } = [];
[ObservableProperty]
public partial string Password { get; set; }
[ObservableProperty]
public partial string Input { get; set; }
#endregion
#region
public ICommand ShowSelectedItemsCommand { get; set; }
public ICommand ToggleDetailsCommand { get; set; }
public ICommand ShowWindowCommand { get; set; }
[RelayCommand]
private void AddArea()
{
MessageBox.Show($"多选1{SelectedListAreas.Count}\n多选2{SelectedObservableAreas.Count}\n单选{Area?.Name}");
//StringBuilder sb = new StringBuilder();
//var dictionary = Application.Current.Resources;
//foreach (var res in dictionary.MergedDictionaries)
//{
// foreach (DictionaryEntry item in res)
// {
// //sb.AppendLine($"{item.Key} = {item.Value}");
// if (item.Key.ToString() == "TextPrimaryBrush")
// {
// MessageBox.Show($"TextPrimaryBrush:{item.Value}");
// }
// if (item.Key.ToString() == "TextPrimaryBrush")
// {
// MessageBox.Show($"TextSecondaryBrush:{item.Value}");
// }
// }
//}
//输出到桌面
//System.IO.File.WriteAllText(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "resources.txt"), sb.ToString());
}
#endregion
#region
public Area[] Areas { get; } = Enumerable.Range(0, 47)
.Select(i => new Area(i, GetJapaneseRegionName(i)))
.ToArray();
public ObservableCollection<RadioItem> Items { get; set; }
public class RadioItem
{
public string Label { get; set; }
public string Value { get; set; }
}
#endregion
public MainWindow()
{
InitializeData();
InitializeCommands();
DataContext = this;
InitializeComponent();
}
#region
private void InitializeData()
{
// 初始化层级数据
HierarchicalData = new ObservableCollection<TreeNodeItem>
{
new("Phase 1: Project Planning", "Alice", "Completed")
{
Children =
{
new("Define project scope", "Alice", "Completed"),
new("Create project timeline", "Bob", "Completed")
}
},
new("Phase 2: Development", "Charlie", "In Progress")
{
Children =
{
new("Backend development", "David", "In Progress")
{
Children =
{
new("Setup database", "David", "Completed"),
new("Develop APIs", "Eve", "In Progress")
}
},
new("Frontend development", "Frank", "Not Started")
}
},
new("Phase 3: Deployment", "Grace", "Not Started")
};
// 初始化人员数据
DataItems = new ObservableCollection<PersonItem>
{
new("John Doe", 30),
new("Jane Smith", 25),
new("Sam Williams", 42),
new("Peter Jones", 55)
};
// 初始化地区数据
Nodes = new ObservableCollection<TreeNodeItem>
{
new("亚洲") { Children = { new("中国"), new("日本"), new("韩国") } },
new("欧洲") { Children = { new("德国"), new("法国") } },
new("北美洲")
};
// 初始化员工数据
InitializeStaffData();
// 初始化单选项
Items = new ObservableCollection<RadioItem>()
{
new RadioItem { Label = "Apple", Value = "Apple" },
new RadioItem { Label = "Pear", Value = "Pear" },
new RadioItem { Label = "Orange", Value = "Orange" }
};
// 初始化选中集合
SelectedDataItems = new ObservableCollection<PersonItem>();
SelectedNodes = new ObservableCollection<object> { Nodes[0].Children[0] }; // 默认选中"中国"
}
private void InitializeStaffData()
{
var alice = new TreeNodeItem("Alice", 30, "Male", "Manager") { IsExpanded = true };
alice.Children.Add(new TreeNodeItem("Alice1", 21, "Male", "Normal")
{
IsExpanded = true,
Children =
{
new("Alice11", 21, "Male", "Normal"),
new("Alice22", 21, "Female", "Normal")
}
});
alice.Children.Add(new TreeNodeItem("Alice2", 22, "Female", "Normal"));
alice.Children.Add(new TreeNodeItem("Alice3", 23, "Female", "Normal"));
var bob = new TreeNodeItem("Bob", 31, "Male", "CEO");
bob.Children.Add(new TreeNodeItem("Bob1", 24, "Female", "Normal"));
bob.Children.Add(new TreeNodeItem("Bob2", 25, "Female", "Normal"));
bob.Children.Add(new TreeNodeItem("Bob3", 26, "Male", "Normal"));
var cyber = new TreeNodeItem("Cyber", 32, "Female", "Leader");
cyber.Children.Add(new TreeNodeItem("Cyber1", 27, "Female", "Normal"));
cyber.Children.Add(new TreeNodeItem("Cyber2", 28, "Female", "Normal"));
StaffList = new ObservableCollection<TreeNodeItem> { alice, bob, cyber };
}
private void InitializeCommands()
{
ShowSelectedItemsCommand = new RelayCommand(ShowSelectedItems);
ToggleDetailsCommand = new RelayCommand<PersonItem>(ToggleDetails);
ShowWindowCommand = new RelayCommand(ShowWindow);
}
#endregion
#region
private void ShowSelectedItems()
{
string message = $"当前选中了 {SelectedDataItems.Count} 项:\n";
message += string.Join("\n", SelectedDataItems.Select(item => item.Name));
MessageBox.Show(message);
}
private static void ToggleDetails(PersonItem? product)
{
product?.IsDetailsVisible = !product.IsDetailsVisible;
}
#endregion
#region
private static string GetJapaneseRegionName(int index)
{
string[] names =
{
"北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県", "群馬県",
"埼玉県", "千葉県", "東京都", "神奈川県", "新潟県", "富山県", "石川県", "福井県", "山梨県", "長野県",
"岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府", "兵庫県", "奈良県", "和歌山県",
"鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県", "香川県", "愛媛県", "高知県", "福岡県",
"佐賀県", "長崎県", "熊本県", "大分県", "宮崎県", "鹿児島県", "沖縄県"
};
return index < names.Length ? names[index] : $"Region{index}";
}
#endregion
#region
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!_loaded) return;
TextMessage.Text = "选项变化" + e.OriginalSource;
}
private bool _loaded = false;
private int currentStep;
private void Window_Loaded(object sender, RoutedEventArgs e) { _loaded = true; }
private void Switch_OnClick(object sender, RoutedEventArgs e) { ThemeManager.SwitchThemeMode(); }
private void Test_OnClick(object sender, RoutedEventArgs e) { new ControlTestWindow().Show(); }
private void Breadcrumb_Navigate(object sender, RoutedPropertyChangedEventArgs<string> e)
{ MessageBox.Show(e.NewValue); }
#region
private void LeftTopButton_Click(object sender, RoutedEventArgs e)
{
Notification.Show("发生错误", "无法连接到服务器,请检查您的网络连接。",
NotificationType.Error, NotificationPlacement.TopLeft, durationSeconds: 5);
}
private void RightTopButton_Click(object sender, RoutedEventArgs e)
{
Notification.Show("操作成功", "您的设置已保存,并已成功应用到系统中。", NotificationType.Success);
}
private void LeftBottomButton_Click(object sender, RoutedEventArgs e)
{
Notification.Show("警告", "您的磁盘空间即将用尽,请及时清理文件。",
NotificationType.Warning, NotificationPlacement.BottomLeft);
}
private void RightBottomButton_Click(object sender, RoutedEventArgs e)
{
Notification.Show("系统提示", "这是一条普通的信息提示,用于通知用户。",
NotificationType.Info, NotificationPlacement.BottomRight);
}
#endregion
#region Toast
private void Info_Click(object sender, RoutedEventArgs e)
{
Toast.Screen.ShowInfo("这是一条桌面通知,显示在屏幕。");
}
private void Success_Click(object sender, RoutedEventArgs e)
{
Toast.For(this).ShowSuccess("操作成功!已在当前窗口内显示。");
}
private void Warning_Click(object sender, RoutedEventArgs e)
{
Toast.Screen.ShowWarning("这是一条桌面通知,请及时续订。");
}
private void Error_Click(object sender, RoutedEventArgs e)
{
Toast.For(this).ShowError("加载失败,请检查您的网络连接。");
}
#endregion
#region Modal
private void ShowBasicModal_Click(object sender, RoutedEventArgs e)
{
bool? result = Modal.Confirm(this, "默认对话框", "默认对话框");
if (result == true)
{
MessageBox.Show("User clicked OK!");
}
}
private void ShowAsyncModal_Click(object sender, RoutedEventArgs e)
{
bool? result = Modal.Confirm(this, "异步对话框", "对话框将在2s后关闭",
async () =>
{
await Task.Delay(2000);
return true;
});
if (result == true)
{
MessageBox.Show("Async task completed and user clicked OK!");
}
}
private void ShowInfoModal_Click(object sender, RoutedEventArgs e)
{
Modal.Info(this, "Information", "This is an information dialog.");
}
#endregion
#region
private void Icon_OnClick(object sender, RoutedEventArgs e)
{
IconsWindow iconsWindow = new IconsWindow
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
iconsWindow.ShowDialog();
}
private async void ThemeToggle_OnCheckedChanged(object sender, RoutedEventArgs e)
{
try
{
if (sender is not ToggleButton) return;
//var targetMode = tb.IsChecked == true ? ThemeMode.Dark : ThemeMode.Light;
//await ThemeManager.ApplyThemeAsync(targetMode, animationDurationMs: 800);
await ThemeManager.SwitchThemeModeAnimatedAsync(800);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private void ColorPalette_OnClick(object sender, RoutedEventArgs e)
{
var palette = new ColorPaletteWindow
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
palette.ShowDialog();
}
private void PrimaryColorSelectComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ComboBox box)
{
ThemePalette palette = box.SelectedIndex switch
{
0 => ThemePalette.Blue,
1 => ThemePalette.Green,
2 => ThemePalette.Purple,
_ => ThemePalette.Blue
};
ThemeManager.ApplyThemePalette(palette);
}
}
#endregion
#endregion
private void ShowNeumorphism_Click(object sender, RoutedEventArgs e)
{
NeumorphismWindow window = new NeumorphismWindow
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
window.ShowDialog();
}
private void MyTutorial_Close(object sender, RoutedEventArgs e)
{
MessageBox.Show("教程已关闭");
ShowCurrentStep();
}
private void MyTutorial_Back(object sender, RoutedEventArgs e)
{
MessageBox.Show("返回上一步");
}
private void StartBtn_Click(object sender, RoutedEventArgs e)
{
currentStep = 1;
ShowCurrentStep();
}
private void MyTutorial_Next(object sender, RoutedEventArgs e)
{
currentStep++;
ShowCurrentStep();
}
private void ShowCurrentStep()
{
switch (currentStep)
{
case 1:
MyTutorial.ShowStep(TargetButton, "保存功能", "点击这里可以保存你当前的进度。", "1 / 2");
break;
case 2:
MyTutorial.ShowStep(StartBtn, "删除功能", "谨慎使用,这会清除数据。", "2 / 2");
break;
default:
MyTutorial.Close();
break;
}
}
private void ShowWindow()
{
this.Show();
this.WindowState = WindowState.Normal;
this.Activate(); // 激活窗口使其置顶
}
// --- 2. 右键菜单的 Click 事件 (Code-behind 风格) ---
private void MenuShow_Click(object sender, RoutedEventArgs e)
{
ShowWindow();
}
private void MenuExit_Click(object sender, RoutedEventArgs e)
{
// 真正退出程序
Application.Current.Shutdown();
}
// --- 3. 重写关闭事件实现“点X最小化到托盘” ---
protected override void OnClosing(CancelEventArgs e)
{
// 阻止窗口真正销毁
e.Cancel = true;
// 隐藏窗口(此时托盘图标依然在运行)
this.Hide();
}
}
#region
/// <summary>
/// 统一的树形节点数据模型,替代 Item、NodeViewModel、Staff、TaskItem
/// </summary>
public partial class TreeNodeItem : ObservableObject
{
[ObservableProperty]
public partial string Name { get; set; } = string.Empty;
[ObservableProperty]
public partial int Age { get; set; }
[ObservableProperty]
public partial string Sex { get; set; } = string.Empty;
[ObservableProperty]
public partial string Duty { get; set; } = string.Empty;
[ObservableProperty]
public partial string Status { get; set; } = string.Empty;
[ObservableProperty]
public partial string Owner { get; set; } = string.Empty;
[ObservableProperty]
public partial bool IsDetailsVisible { get; set; } = false;
[ObservableProperty]
public partial bool IsChecked { get; set; } = true;
[ObservableProperty]
public partial bool IsSelected { get; set; } = false;
[ObservableProperty]
public partial bool IsExpanded { get; set; } = false;
public ObservableCollection<TreeNodeItem> Children { get; set; } = new();
public TreeNodeItem() { }
public TreeNodeItem(string name) : this()
{
Name = name;
}
public TreeNodeItem(string name, string owner, string status) : this(name)
{
Owner = owner;
Status = status;
}
public TreeNodeItem(string name, int age, string sex, string duty) : this(name)
{
Age = age;
Sex = sex;
Duty = duty;
}
}
/// <summary>
/// 简化的人员数据模型
/// </summary>
public partial class PersonItem : ObservableObject
{
[ObservableProperty]
public partial string Name { get; set; } = string.Empty;
[ObservableProperty]
public partial int Age { get; set; }
[ObservableProperty]
public partial bool IsDetailsVisible { get; set; } = false;
public PersonItem() { }
public PersonItem(string name, int age)
{
Name = name;
Age = age;
}
}
public class Area
{
public Area()
{
}
public Area(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
public int Id { get; set; }
public string Name { get; set; }
public override string ToString() => Name;
}
#endregion