代码优化
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
using System.Net.Sockets;
|
||||
using 头罩视野.Services.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using Microsoft.Win32;
|
||||
|
||||
public static class ModbusHelper
|
||||
{
|
||||
// 全局唯一连接,所有页面共用
|
||||
@@ -14,4 +21,49 @@ public static class ModbusHelper
|
||||
|
||||
// 判断是否连接成功
|
||||
public static bool IsConnected => TcpClient != null && TcpClient.Connected;
|
||||
}
|
||||
|
||||
|
||||
// <summary>
|
||||
/// 公共保存方法(用户自选文件夹)
|
||||
/// </summary>
|
||||
/// <param name="dataList">你的数据集合</param>
|
||||
/// <param name="defaultFileName">默认文件名</param>
|
||||
public static void SaveToCsv(List<dynamic> dataList, string defaultFileName)
|
||||
{
|
||||
if (dataList == null || dataList.Count == 0)
|
||||
{
|
||||
MessageBox.Show("无数据可保存!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 选择文件夹
|
||||
var folderDialog = new OpenFolderDialog
|
||||
{
|
||||
Title = "请选择保存路径"
|
||||
};
|
||||
|
||||
if (folderDialog.ShowDialog() != true)
|
||||
return;
|
||||
|
||||
string folderPath = folderDialog.FolderName;
|
||||
string filePath = Path.Combine(folderPath, defaultFileName);
|
||||
|
||||
// 写入 CSV
|
||||
using (var sw = new StreamWriter(filePath, false, Encoding.UTF8))
|
||||
{
|
||||
var firstRow = (IDictionary<string, object>)dataList[0];
|
||||
sw.WriteLine(string.Join(",", firstRow.Keys));
|
||||
|
||||
foreach (var item in dataList)
|
||||
{
|
||||
var dict = (IDictionary<string, object>)item;
|
||||
sw.WriteLine(string.Join(",", dict.Values));
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("保存成功!\n" + filePath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//保存方法
|
||||
@@ -90,45 +90,15 @@ namespace 头罩视野.Views
|
||||
// 保存左眼
|
||||
private void btnSaveLeft_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SaveToCsv(LeftEyeDataList, $"左眼数据_{DateTime.Now:yyyyMMddHHmmss}.csv");
|
||||
//SaveToCsv(LeftEyeDataList, $"左眼数据_{DateTime.Now:yyyyMMddHHmmss}.csv");
|
||||
ModbusHelper.SaveToCsv(LeftEyeDataList, $"左眼数据_{DateTime.Now:yyyyMMddHHmmss}.csv");
|
||||
}
|
||||
|
||||
|
||||
// 保存右眼
|
||||
private void btnSaveRight_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SaveToCsv(RightEyeDataList, $"右眼数据_{DateTime.Now:yyyyMMddHHmmss}.csv");
|
||||
}
|
||||
|
||||
private void SaveToCsv(List<dynamic> dataList, string defaultFileName)
|
||||
{
|
||||
if (dataList == null || dataList.Count == 0)
|
||||
{
|
||||
MessageBox.Show("无数据可保存");
|
||||
return;
|
||||
}
|
||||
|
||||
// 打开文件夹选择框
|
||||
var folderDialog = new OpenFolderDialog();
|
||||
if (folderDialog.ShowDialog() != true)
|
||||
return;
|
||||
|
||||
string folder = folderDialog.FolderName;
|
||||
string path = System.IO.Path.Combine(folder, defaultFileName);
|
||||
|
||||
// 写入 CSV
|
||||
using (var sw = new StreamWriter(path, false, Encoding.UTF8))
|
||||
{
|
||||
var first = (IDictionary<string, object>)dataList[0];
|
||||
sw.WriteLine(string.Join(",", first.Keys));
|
||||
|
||||
foreach (var item in dataList)
|
||||
{
|
||||
var dict = (IDictionary<string, object>)item;
|
||||
sw.WriteLine(string.Join(",", dict.Values));
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("保存成功:\n" + path);
|
||||
ModbusHelper.SaveToCsv(RightEyeDataList,$"右眼数据_{DateTime.Now:yyyyMMddHHmmss}.csv");
|
||||
}
|
||||
|
||||
|
||||
@@ -164,18 +134,6 @@ namespace 头罩视野.Views
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
|
||||
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
|
||||
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
|
||||
|
||||
Reference in New Issue
Block a user