代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
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;
using Path = System.IO.Path;
using ScriptBox.Services;
namespace ScriptBox
{
/// <summary>
/// Window_DbcBalance.xaml 的交互逻辑
/// </summary>
// 在类顶部确保实现INotifyPropertyChanged接口
public partial class Window_DbcBalance : Window, INotifyPropertyChanged
{
// 添加时间槽模式枚举
public enum TimeSlotMode { FiveMS, TenMS }
private TimeSlotMode _selectedTimeSlot = TimeSlotMode.FiveMS;
public TimeSlotMode SelectedTimeSlot
{
get => _selectedTimeSlot;
set
{
_selectedTimeSlot = value;
OnPropertyChanged();
}
}
private ObservableCollection<string> _filePaths = new ObservableCollection<string>();
public ObservableCollection<string> FilePaths
{
get => _filePaths;
set
{
_filePaths = value;
OnPropertyChanged();
OnPropertyChanged(nameof(HasFiles));
OnPropertyChanged(nameof(FileCount));
}
}
public bool HasFiles => FilePaths.Any();
public string FileCount => FilePaths.Count.ToString();
public Window_DbcBalance()
{
InitializeComponent();
DataContext = this;
InitializeDropHandlers();
}
private void InitializeDropHandlers()
{
DropZone.AllowDrop = true;
const string validExtension = ".dbc";
DropZone.DragEnter += (s, e) =>
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
var invalidFiles = files
.Where(f => !Path.GetExtension(f).Equals(validExtension, StringComparison.OrdinalIgnoreCase))
.Count();
if (invalidFiles == 0)
{
StatusText.Text = "释放鼠标导入.dbc文件";
DropZone.Background = Brushes.LightGreen;
e.Effects = DragDropEffects.Copy;
}
else
{
StatusText.Text = $"包含 {invalidFiles} 个非.dbc文件";
DropZone.Background = Brushes.LightPink;
e.Effects = DragDropEffects.None;
}
}
else
{
StatusText.Text = "仅支持文件拖放操作";
DropZone.Background = Brushes.LightPink;
e.Effects = DragDropEffects.None;
}
};
DropZone.Drop += (s, e) =>
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = ((string[])e.Data.GetData(DataFormats.FileDrop))
.Where(f => Path.GetExtension(f).Equals(validExtension, StringComparison.OrdinalIgnoreCase))
.Distinct()
.ToList();
if (files.Count > 0)
{
// 修改集合更新方式
FilePaths.Clear();
foreach (var file in files)
{
FilePaths.Add(file);
}
StatusText.Text = $"已选择 {files.Count} 个.dbc文件";
// 添加双重通知
OnPropertyChanged(nameof(FilePaths));
OnPropertyChanged(nameof(HasFiles));
}
}
DropZone.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
};
}
private void ConvertButton_Click(object sender, RoutedEventArgs e)
{
try
{
var successCount = 0;
var errorMessages = new List<string>(); // 新增错误收集器
foreach (var filePath in FilePaths)
{
if (!File.Exists(filePath))
{
errorMessages.Add($"文件不存在:{filePath}");
continue;
}
try
{
ProcessFile(filePath);
successCount++;
}
catch (Exception ex)
{
errorMessages.Add($"{Path.GetFileName(filePath)}: {ex.Message}"); // 收集具体错误
}
}
// 构建最终状态信息
var statusBuilder = new StringBuilder();
if (errorMessages.Any())
{
statusBuilder.AppendLine("以下文件处理失败:");
statusBuilder.AppendLine(string.Join("\n", errorMessages));
}
statusBuilder.Append($"成功转换 {successCount}/{FilePaths.Count} 个文件");
StatusText.Text = statusBuilder.ToString();
}
catch (Exception ex)
{
StatusText.Text = $"系统错误:{ex.Message}";
}
}
// 修改方法签名,抛出异常而不是内部处理
private void ProcessFile(string inputPath)
{
var processor = new DBCProcessor(inputPath,
SelectedTimeSlot == TimeSlotMode.FiveMS ? 5 : 10);
processor.ProcessFile(out string outputPath, out string reportPath);
if (!File.Exists(outputPath) || !File.Exists(reportPath))
{
throw new InvalidOperationException("输出文件生成失败"); // 抛出异常
}
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
FilePaths.Clear();
StatusText.Text = "文件列表已清空";
}
// INotifyPropertyChanged 实现
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "") // 参数改为空字符串默认值
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// 添加RadioButton绑定
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (sender is RadioButton radio)
{
if (radio.Content.ToString() == "5ms")
SelectedTimeSlot = TimeSlotMode.FiveMS;
else if (radio.Content.ToString() == "10ms")
SelectedTimeSlot = TimeSlotMode.TenMS;
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。