C# WPF中添加调试信息查看窗体
第一步:添加wpf窗口;第二步:在主窗体image的MouseLeftButtonUp事件中调用调试窗口;
StatusViewWindow svWindow = new StatusViewWindow();
svWindow.Show();
第三步:在主窗体中开个线程通过udp接收光电广开的数据:
Thread t3 = new Thread(StartSwitchDataRevThread);//四个广电开关数据接受线程
t3.Name = "StartSwitchDataRevThread";
t3.Start();
t3.IsBackground = true;
private void StartSwitchDataRevThread()
{
try
{
Stopwatch elapsetime = new Stopwatch();
UdpClient client = new UdpClient(8012);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.1.202"), 8008);
client.Client.ReceiveBufferSize = 1024 * 1024;//默认值是8192
while (true)
{
Byte[] recv;
recv = client.Receive(ref endpoint);
for (int i = 0; i < 4; i++)
{
switchSignal = recv;
}
switchList.Add(switchSignal);
elapsetime.Restart();//计时开始
if (switchDataWrite == true)
{
String switchDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "switchData");
string stringData = "0x" + BitConverter.ToString(recv).Replace("-", " 0x").ToLower();
stringData = DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss:fff") + "" + stringData;
StrWrite.strWrite(stringData, switchDataPath, "switchDataFile.txt");
}
elapsetime.Stop();//计时结束
Console.WriteLine("接收数据耗时:" + elapsetime.ElapsedMilliseconds.ToString("0000"));
}
}
catch (Exception ex)
{
LogWrite.logWrite(ex.Message, ex.StackTrace);
}
}
第四部:在StatusViewWindow窗口定义一个10毫秒的定时器,每隔十毫秒从主窗体获取一次光电开关的数据并显示到listBox1中;
<Window x:Class="thzSoftware.StatusViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:thzSoftware"
mc:Ignorable="d"
Title="StatusViewWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid ShowGridLines="False" Background="LightCyan" >
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Name="listBox1" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Background="LightGray"/>
<Button Name="btnCtrl1" Content="开始采集" FontSize="30" Grid.Row="0" Grid.Column="0" Background="LightGray" Click="BtnCtrl1_Click"/>
<Button Name="btnCtrl2" Content="停止采集" FontSize="30" Grid.Row="1" Grid.Column="0" Background="LightGray" Click="BtnCtrl2_Click"/>
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Threading;
namespace thzSoftware
{
/// <summary>
/// StatusViewWindow.xaml 的交互逻辑
/// </summary>
public partial class StatusViewWindow : Window
{
public StatusViewWindow()
{
InitializeComponent();
}
Byte[] switchSignal = new byte;
DispatcherTimer StatusViewTimer;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
StatusViewTimer = new DispatcherTimer();
StatusViewTimer.Interval = TimeSpan.FromMilliseconds(10);
StatusViewTimer.Tick += StatusViewTimer_Tick;//加载事件,敲tab键事件框架可以自己出来
}
private void StatusViewTimer_Tick(object sender, EventArgs e)
{
switchSignal = thzModel.DataProcess.switchSignal;
string stringData = "0x" + BitConverter.ToString(switchSignal).Replace("-", " 0x").ToLower();
stringData = DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss:fff") + "" + stringData;
listBox1.Items.Add(stringData);
}
private void BtnCtrl1_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
StatusViewTimer.Start();
}
private void BtnCtrl2_Click(object sender, RoutedEventArgs e)
{
StatusViewTimer.Stop();
}
}
}
运行结果如下;
欢迎关注公众号: dotnet编程大全
https://blog.51cto.com/u_15383815/4863228
页:
[1]