优化
This commit is contained in:
467
NeoUI/NeoUITest/MainWindow.xaml.cs
Normal file
467
NeoUI/NeoUITest/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,467 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
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 NeoUI.Appearance;
|
||||
using NeoUI.Controls;
|
||||
|
||||
namespace NeoUITest;
|
||||
|
||||
/// <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]
|
||||
private TreeNodeItem selectedItem;
|
||||
|
||||
[ObservableProperty]
|
||||
private Area area;
|
||||
|
||||
[ObservableProperty]
|
||||
public partial List<Area> SelectedListAreas { get; set; } = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private string password;
|
||||
|
||||
[ObservableProperty]
|
||||
private string input;
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand ShowSelectedItemsCommand { get; set; }
|
||||
public ICommand ToggleDetailsCommand { get; set; }
|
||||
|
||||
[RelayCommand]
|
||||
private void AddArea() { }
|
||||
#endregion
|
||||
|
||||
#region 静态数据
|
||||
public Area[] Areas { get; } = Enumerable.Range(0, 47)
|
||||
.Select(i => new Area(i, GetJapaneseRegionName(i)))
|
||||
.ToArray();
|
||||
|
||||
public IEnumerable<RadioItem> Items { get; set; }
|
||||
|
||||
public struct RadioItem
|
||||
{
|
||||
public string Label;
|
||||
public string Value;
|
||||
}
|
||||
#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[]
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private void ToggleDetails(PersonItem product)
|
||||
{
|
||||
if (product != null)
|
||||
{
|
||||
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 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)
|
||||
{
|
||||
NotificationManager.Show("发生错误", "无法连接到服务器,请检查您的网络连接。",
|
||||
NotificationType.Error, NotificationPlacement.TopLeft, durationSeconds: 5);
|
||||
}
|
||||
|
||||
private void RightTopButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.Show("操作成功", "您的设置已保存,并已成功应用到系统中。", NotificationType.Success);
|
||||
}
|
||||
|
||||
private void LeftBottomButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.Show("警告", "您的磁盘空间即将用尽,请及时清理文件。",
|
||||
NotificationType.Warning, NotificationPlacement.BottomLeft);
|
||||
}
|
||||
|
||||
private void RightBottomButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.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, "Basic Dialog", "This is a basic modal dialog.");
|
||||
if (result == true)
|
||||
{
|
||||
MessageBox.Show("User clicked OK!");
|
||||
}
|
||||
}
|
||||
|
||||
private async void ShowAsyncModal_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
bool? result = Modal.Confirm(this, "Async Dialog", "This dialog will close after a 2-second task.",
|
||||
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 bool _themeAnimating;
|
||||
|
||||
private async void ThemeToggle_OnCheckedChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_themeAnimating) return;
|
||||
if (sender is not ToggleButton tb) return;
|
||||
|
||||
var targetMode = tb.IsChecked == true ? ThemeMode.Dark : ThemeMode.Light;
|
||||
|
||||
try
|
||||
{
|
||||
_themeAnimating = true;
|
||||
await ThemeManager.ApplyThemeAsync(targetMode, animate: true, animationDurationMs: 2000);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("[ThemeToggle] " + ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_themeAnimating = false;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#region 数据模型
|
||||
/// <summary>
|
||||
/// 统一的树形节点数据模型,替代 Item、NodeViewModel、Staff、TaskItem
|
||||
/// </summary>
|
||||
public partial class TreeNodeItem : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string name = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private int age;
|
||||
|
||||
[ObservableProperty]
|
||||
private string sex = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string duty = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string status = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string owner = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isDetailsVisible = false;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isChecked = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isSelected = false;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isExpanded = 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]
|
||||
private string name = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private int age;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isDetailsVisible = false;
|
||||
|
||||
public PersonItem() { }
|
||||
|
||||
public PersonItem(string name, int age)
|
||||
{
|
||||
Name = name;
|
||||
Age = age;
|
||||
}
|
||||
}
|
||||
|
||||
public class Area
|
||||
{
|
||||
public Area(int Id, string Name)
|
||||
{
|
||||
this.Id = Id;
|
||||
this.Name = Name;
|
||||
}
|
||||
|
||||
public int Id { get; }
|
||||
public string Name { get; }
|
||||
|
||||
public override string ToString() => Name;
|
||||
}
|
||||
#endregion
|
||||
Reference in New Issue
Block a user