C# WPF中添加调试信息查看窗体

第一步:添加wpf窗口;

bf873cbede553043e80ce9a08b6752e4.png

第二步:在主窗体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[i] = recv[i + 2];
                        
                    }
                    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[4];
        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();
        }
    }
}

运行结果如下;

c94aa761701cccaa4dbcdd0d1e10234b.png

欢迎关注公众号: dotnet编程大全

技术群: 需要进技术群的添加小编微信mm1552923,备注:加群;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zls365365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值