生活随笔
收集整理的這篇文章主要介紹了
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;//默認值是8192while (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();}}
}
運行結果如下;
總結
以上是生活随笔為你收集整理的C# WPF中添加调试信息查看窗体的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。