Files
Shrlalgo.RvKits/MelskinTest/MainWindow.xaml.cs

565 lines
17 KiB
C#
Raw Permalink Normal View History

2025-08-24 13:49:55 +08:00
using System;
using System.Collections;
2025-08-24 13:49:55 +08:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
2025-12-23 21:35:54 +08:00
using System.Diagnostics;
2025-08-24 13:49:55 +08:00
using System.Linq;
using System.Text;
2025-08-24 13:49:55 +08:00
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;
2026-01-02 17:30:41 +08:00
using Melskin.Appearance;
using Melskin.Controls;
2025-08-24 13:49:55 +08:00
2026-02-17 22:17:23 +08:00
namespace MelskinTest;
2025-08-24 13:49:55 +08:00
/// <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; }
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial Area AutoArea { get; set; }
2026-02-12 21:29:00 +08:00
[ObservableProperty]
public partial Area SearchArea { get; set; }
partial void OnSearchAreaChanged(Area value)
{
MessageBox.Show($"搜索选择了:{value?.Name}");
}
2025-12-23 21:35:54 +08:00
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();
}
}
2025-08-24 13:49:55 +08:00
[ObservableProperty]
public partial List<Area> SelectedListAreas { get; set; } = [];
[ObservableProperty]
public partial string Password { get; set; }
2025-08-24 13:49:55 +08:00
[ObservableProperty]
public partial string Input { get; set; }
2025-08-24 13:49:55 +08:00
#endregion
#region
public ICommand ShowSelectedItemsCommand { get; set; }
public ICommand ToggleDetailsCommand { get; set; }
[RelayCommand]
private void AddArea()
{
2025-12-23 21:35:54 +08:00
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());
}
2025-08-24 13:49:55 +08:00
#endregion
#region
public Area[] Areas { get; } = Enumerable.Range(0, 47)
.Select(i => new Area(i, GetJapaneseRegionName(i)))
.ToArray();
2025-09-04 22:39:00 +08:00
public ObservableCollection<RadioItem> Items { get; set; }
2025-08-24 13:49:55 +08:00
2025-09-04 22:39:00 +08:00
public class RadioItem
2025-08-24 13:49:55 +08:00
{
2025-09-04 22:39:00 +08:00
public string Label { get; set; }
public string Value { get; set; }
2025-08-24 13:49:55 +08:00
}
#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();
// 初始化单选项
2025-09-04 22:39:00 +08:00
Items = new ObservableCollection<RadioItem>()
2025-08-24 13:49:55 +08:00
{
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);
}
#endregion
#region
private void ShowSelectedItems()
{
string message = $"当前选中了 {SelectedDataItems.Count} 项:\n";
message += string.Join("\n", SelectedDataItems.Select(item => item.Name));
MessageBox.Show(message);
}
2025-12-23 21:35:54 +08:00
private static void ToggleDetails(PersonItem? product)
2025-08-24 13:49:55 +08:00
{
2025-12-23 21:35:54 +08:00
product?.IsDetailsVisible = !product.IsDetailsVisible;
2025-08-24 13:49:55 +08:00
}
#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;
2026-02-22 20:03:42 +08:00
private int currentStep;
2025-08-24 13:49:55 +08:00
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)
{
2025-12-23 21:35:54 +08:00
Notification.Show("发生错误", "无法连接到服务器,请检查您的网络连接。",
2025-08-24 13:49:55 +08:00
NotificationType.Error, NotificationPlacement.TopLeft, durationSeconds: 5);
}
private void RightTopButton_Click(object sender, RoutedEventArgs e)
{
2025-12-23 21:35:54 +08:00
Notification.Show("操作成功", "您的设置已保存,并已成功应用到系统中。", NotificationType.Success);
2025-08-24 13:49:55 +08:00
}
private void LeftBottomButton_Click(object sender, RoutedEventArgs e)
{
2025-12-23 21:35:54 +08:00
Notification.Show("警告", "您的磁盘空间即将用尽,请及时清理文件。",
2025-08-24 13:49:55 +08:00
NotificationType.Warning, NotificationPlacement.BottomLeft);
}
private void RightBottomButton_Click(object sender, RoutedEventArgs e)
{
2025-12-23 21:35:54 +08:00
Notification.Show("系统提示", "这是一条普通的信息提示,用于通知用户。",
2025-08-24 13:49:55 +08:00
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, "默认对话框", "默认对话框");
2025-08-24 13:49:55 +08:00
if (result == true)
{
MessageBox.Show("User clicked OK!");
}
}
private void ShowAsyncModal_Click(object sender, RoutedEventArgs e)
2025-08-24 13:49:55 +08:00
{
bool? result = Modal.Confirm(this, "异步对话框", "对话框将在2s后关闭",
2025-08-24 13:49:55 +08:00
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)
{
2025-12-23 21:35:54 +08:00
try
{
if (sender is not ToggleButton) return;
2025-08-24 13:49:55 +08:00
2025-12-23 21:35:54 +08:00
//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);
}
2025-08-24 13:49:55 +08:00
}
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
2025-10-10 11:19:58 +08:00
2025-08-24 13:49:55 +08:00
#endregion
2025-10-10 11:19:58 +08:00
2025-12-28 11:47:54 +08:00
private void ShowNeumorphism_Click(object sender, RoutedEventArgs e)
{
NeumorphismWindow window = new NeumorphismWindow
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
window.ShowDialog();
}
2026-02-22 20:03:42 +08:00
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;
}
}
2025-08-24 13:49:55 +08:00
}
#region
/// <summary>
/// 统一的树形节点数据模型,替代 Item、NodeViewModel、Staff、TaskItem
/// </summary>
public partial class TreeNodeItem : ObservableObject
{
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial string Name { get; set; } = string.Empty;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial int Age { get; set; }
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial string Sex { get; set; } = string.Empty;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial string Duty { get; set; } = string.Empty;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial string Status { get; set; } = string.Empty;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial string Owner { get; set; } = string.Empty;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial bool IsDetailsVisible { get; set; } = false;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial bool IsChecked { get; set; } = true;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial bool IsSelected { get; set; } = false;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial bool IsExpanded { get; set; } = false;
2025-08-24 13:49:55 +08:00
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]
2025-12-23 21:35:54 +08:00
public partial string Name { get; set; } = string.Empty;
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial int Age { get; set; }
2025-08-24 13:49:55 +08:00
[ObservableProperty]
2025-12-23 21:35:54 +08:00
public partial bool IsDetailsVisible { get; set; } = false;
2025-08-24 13:49:55 +08:00
public PersonItem() { }
public PersonItem(string name, int age)
{
Name = name;
Age = age;
}
}
public class Area
{
2025-12-23 21:35:54 +08:00
public Area()
{
2026-02-22 20:03:42 +08:00
2025-12-23 21:35:54 +08:00
}
2025-08-24 13:49:55 +08:00
public Area(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
2025-12-23 21:35:54 +08:00
public int Id { get; set; }
public string Name { get; set; }
2025-08-24 13:49:55 +08:00
public override string ToString() => Name;
}
#endregion