Files
ShrlAlgoToolkit/ShrlAlgoStudio/GuidGeneratorView.xaml.cs
2026-02-17 22:17:23 +08:00

78 lines
2.2 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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ShrlAlgoStudio
{
/// <summary>
/// GuidGeneratorView.xaml 的交互逻辑
/// </summary>
public partial class GuidGeneratorView
{
public GuidGeneratorView()
{
InitializeComponent();
GenerateGuid();
}
private void BtnGenerate_Click(object sender, RoutedEventArgs e)
{
GenerateGuid();
}
private void BtnCopy_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(ResultTextBox.Text))
{
Clipboard.SetText(ResultTextBox.Text);
StatusText.Text = "已复制到剪贴板!";
StatusText.Foreground = Brushes.Green;
}
}
private void GenerateGuid()
{
// 1. 生成新的 GUID
Guid newGuid = Guid.NewGuid();
// 2. 获取用户选择的格式
// 从 ComboBoxItem 的 Tag 属性中获取格式代码 (D, N, B, P, X)
string format = "D"; // 默认为 D
if (FormatComboBox.SelectedItem is ComboBoxItem selectedItem && selectedItem.Tag != null)
{
format = selectedItem.Tag.ToString();
}
// 3. 格式化字符串
string result = newGuid.ToString(format);
// 4. 处理大小写
// 注意Hex(X) 格式通常包含大括号和0xToUpper会全部大写符合常规需求
if (UpperCaseCheckBox.IsChecked == true)
{
result = result.ToUpper();
}
else
{
result = result.ToLower();
}
// 5. 显示结果
ResultTextBox.Text = result;
// 重置状态栏提示
StatusText.Text = "生成成功";
StatusText.Foreground = Brushes.Black;
}
}
}