将数据从 UWA 流式传输到 WPF
本文关键字:传输 WPF 数据 UWA | 更新日期: 2023-09-27 18:34:02
我有一个通用的Windows应用程序,我在其中实时收集Microsoft频段传感器数据。现在,我想将这些传感器数据流式传输到 WPF 应用程序。我最初认为 AppServiceConnection 类可能是最好的方法,但我似乎无法理解如何使用这个通信过程,当我只想要服务提供商获取传感器数据并持续流式传输它的实时通信,而客户端接收和显示数据。
下面是一些代码:客户:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
namespace Socket_Communication_UWA
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private StreamSocket clientSocket;
private HostName serverHost;
//private string serverHostnameString;
//private string serverPort;
private bool connected = false;
private bool closing = false;
public MainPage()
{
this.InitializeComponent();
clientSocket = new StreamSocket();
}
private async void connect(object sender, RoutedEventArgs e)
{
if (connected)
{
StatusText.Text = "Already connected";
return;
}
try
{
OutputView.Text = "";
StatusText.Text = "Trying to connect ...";
serverHost = new HostName(ServerHostname.Text);
// Try to connect to the
await clientSocket.ConnectAsync(serverHost, ServerPort.Text);
connected = true;
StatusText.Text = "Connection established" + Environment.NewLine;
}
catch (Exception exception)
{
// If this is an unknown status,
// it means that the error is fatal and retry will likely fail.
if (Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
StatusText.Text = "Connect failed with error: " + exception.Message;
// Could retry the connection, but for this simple example
// just close the socket.
closing = true;
// the Close method is mapped to the C# Dispose
clientSocket.Dispose();
clientSocket = null;
}
}
private async void Send_Click(object sender, RoutedEventArgs e)
{
if (!connected)
{
StatusText.Text = "Must be connected to send!";
return;
}
UInt32 len = 0; // Gets the UTF-8 string length.
try
{
OutputView.Text = "";
StatusText.Text = "Trying to send data ...";
// add a newline to the text to send
string sendData = SendText.Text + Environment.NewLine;
DataWriter writer = new DataWriter(clientSocket.OutputStream);
len = writer.MeasureString(sendData); // Gets the UTF-8 string length.
// Call StoreAsync method to store the data to a backing stream
await writer.StoreAsync();
StatusText.Text = "Data was sent" + Environment.NewLine;
// await writer.FlushAsync();
// writer.WriteString("go" + Environment.NewLine);
// detach the stream and close it
await writer.FlushAsync();
writer.DetachStream();
writer.Dispose();
closing = true;
clientSocket.Dispose();
clientSocket = null;
connected = false;
}
catch (Exception exception)
{
// If this is an unknown status,
// it means that the error is fatal and retry will likely fail.
if (Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
StatusText.Text = "Send data or receive failed with error: " + exception.Message;
// Could retry the connection, but for this simple example
// just close the socket.
closing = true;
clientSocket.Dispose();
clientSocket = null;
connected = false;
}
}
}
}
听者:
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;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
using System.Threading;
namespace Socket_listener
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string line;
public MainWindow()
{
InitializeComponent();
}
public async void button_start_Click(object sender, RoutedEventArgs e)
{
Int32 port = 4510;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(localAddr, port);
listener.Start();
using (TcpClient client = await listener.AcceptTcpClientAsync())
using (StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8))
{
while ((line = reader.ReadLine()) != null)
{
Thread.Sleep(10);
line = reader.ReadLine();
Console.WriteLine("This is the received message " + line);
//textBox.Text = line;
// client.Close();
}
// Console.WriteLine("This is the received message " + line);
}
listener.Stop();
}
}
}
你可以使用 SignalR lib 来实现此目的。我已经成功地将其与SCADA网络同时与WPF和Web应用程序结合使用。
可以在以下位置找到有关此库的详细信息:SignalR Information、SignalR ASP.NET Site