页面代码逻辑调整

This commit is contained in:
2026-04-21 10:35:43 +08:00
parent e021559ca9
commit 993f75f292
3 changed files with 41 additions and 5 deletions

View File

@@ -1,4 +1,6 @@
//using DateRecordView;
using Modbus.Device;
using System.Net.Sockets;
using System.Text;
using System.Windows;
using System.Windows.Controls;
@@ -9,6 +11,8 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using .Services;
using .Services.Data;
using .Views;
@@ -17,9 +21,11 @@ namespace 头罩视野
public partial class MainWindow : Window
{
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
public MainWindow()
{
InitializeComponent();
InitializeModbusTcp();
//MainFrame.Content = new Views.ChangeLanguage();
}
@@ -32,5 +38,36 @@ namespace 头罩视野
//NavigationService.Navigate(new Views.RecordDate()); 页面相互跳转
private void InitializeModbusTcp()
{
try
{
string plcIp = "192.168.1.10";
bool initSuccess = ModbusResourceManager.Instance.Init(plcIp, 502);
if (!initSuccess)
{
MessageBox.Show("连接Modbus服务器失败", "错误");
//this.Close();
return;
}
// 检查连接状态
if (_tcpClient == null || !_tcpClient.Connected)
{
MessageBox.Show("Modbus连接异常", "错误");
return;
}
}
catch (Exception ex)
{
ShowError($"Modbus初始化失败: {ex.Message}");
}
}
private void ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

View File

@@ -20,9 +20,6 @@ using 头罩视野.Services;
using .Services.Data;
namespace .Views
{
/// <summary>
/// PageTest.xaml 的交互逻辑
/// </summary>
public partial class PageTest : Page
{
@@ -31,16 +28,19 @@ namespace 头罩视野.Views
DispatcherTimer _timer;
DataChange c = new DataChange();
Function ma;
//// 定时采集用
private DispatcherTimer testTimer;
// 保存上一条数据(用于去重)
private TestDataStore.TestRecord _lastRecord;
public PageTest()
{
InitializeComponent();
_timer = InitDispatcherTimer();
// 2. 初始化定时器500毫秒 执行一次
testTimer = new DispatcherTimer();
testTimer.Interval = TimeSpan.FromMilliseconds(500); // 500ms = 0.5秒
testTimer.Tick += Timer_Tick;
@@ -121,7 +121,6 @@ namespace 头罩视野.Views
{
testTimer?.Stop();
}
//停止btn
private void Button_Click_Stop(object sender, RoutedEventArgs e)
{

View File

@@ -85,7 +85,7 @@ namespace 头罩视野.Views
private void Page_Loaded(object sender, RoutedEventArgs e)
{
//进入页面是否要保留原来的数据????,
RecordDataGrid.ItemsSource = null;
RecordDataGrid.ItemsSource = TestDataStore.Records;