0
我真的不熟悉C#與WPF,我試圖編寫一個程序來顯示實時UDP消息(基本上只是一個值),但是我不能將UDP用我的c#代碼爲WPF監聽代碼,如果我像使用UDP監聽函數一樣,它沒有彈出窗口,任何想法如何操作?萬分感激!C#WPF實時UDP消息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//UDP
using System.Net;
using System.Net.Sockets;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string Rawdata_str1 = "0";
public MainWindow()
{
InitializeComponent();
UDP_listening_PI1();
X_values.DataContext = new X_Y() { X = Rawdata_str1 };
Y_values.DataContext = new X_Y() { Y = Rawdata_str1 };
}
public void UDP_listening_PI1()
{
UdpClient listener = new UdpClient(48911);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, (48911));
bool done = false;
try
{
while (!done)
{
byte[] pdata = listener.Receive(ref groupEP);
string price = Encoding.ASCII.GetString(pdata);
int Rawdata1 = int.Parse(price);
Rawdata_str1 = Rawdata1.ToString();
}
}
finally
{
listener.Close();
}
}
}
public class X_Y
{
public string X { get; set; }
public string Y { get; set; }
}
}
謝謝!它確實工作! – tedhan