C# 从UWA到WPF的数据流

C# 从UWA到WPF的数据流,c#,wpf,C#,Wpf,我有一个通用的Windows应用程序,我在其中实时收集Microsoft波段传感器数据。现在,我想将这些传感器数据流传输到WPF应用程序。我最初认为AppServiceConnection类可能是最好的方式,但当我只想要实时通信时,我似乎不明白如何使用这个通信过程,在这种通信中,服务提供商获取传感器数据并不断地流式传输,而客户机接收并显示数据 下面是一些代码: 客户: 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq;

我有一个通用的Windows应用程序,我在其中实时收集Microsoft波段传感器数据。现在,我想将这些传感器数据流传输到WPF应用程序。我最初认为AppServiceConnection类可能是最好的方式,但当我只想要实时通信时,我似乎不明白如何使用这个通信过程,在这种通信中,服务提供商获取传感器数据并不断地流式传输,而客户机接收并显示数据

下面是一些代码: 客户:

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用System.Net.Sockets;
使用系统线程;
使用系统文本;
使用Windows.Networking;
使用Windows.Networking.Sockets;
使用Windows.Storage.Streams;
命名空间套接字\u通信\u UWA
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
私有StreamSocket客户端套接字;
私有主机名服务器主机;
//私有字符串serverHostnameString;
//私有字符串服务器端口;
私有布尔连接=假;
私有布尔关闭=假;
公共主页()
{
this.InitializeComponent();
clientSocket=newstreamsocket();
}
专用异步void connect(对象发送方,RoutedEventArgs e)
{
如果(已连接)
{
StatusText.Text=“已连接”;
返回;
}
尝试
{
OutputView.Text=“”;
StatusText.Text=“正在尝试连接…”;
serverHost=新主机名(ServerHostname.Text);
//尝试连接到
等待clientSocket.ConnectAsync(serverHost,ServerPort.Text);
连接=真;
StatusText.Text=“已建立连接”+Environment.NewLine;
}
捕获(异常)
{
//如果这是未知状态,
//这意味着错误是致命的,重试可能会失败。
if(Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult)==SocketErrorStatus.Unknown)
{
投掷;
}
StatusText.Text=“连接失败,错误:”+异常消息;
//无法重试连接,但对于这个简单的示例
//把插座合上。
关闭=真;
//Close方法映射到C#Dispose
clientSocket.Dispose();
clientSocket=null;
}
}
私有异步无效发送\单击(对象发送方,路由目标)
{
如果(!已连接)
{
StatusText.Text=“必须连接才能发送!”;
返回;
}
UInt32 len=0;//获取UTF-8字符串长度。
尝试
{
OutputView.Text=“”;
StatusText.Text=“正在尝试发送数据…”;
//在要发送的文本中添加换行符
字符串sendData=SendText.Text+Environment.NewLine;
DataWriter writer=新的DataWriter(clientSocket.OutputStream);
len=writer.MeasureString(sendData);//获取UTF-8字符串长度。
//调用StoreAsync方法将数据存储到备份流
等待writer.StoreAsync();
StatusText.Text=“数据已发送”+Environment.NewLine;
//等待writer.FlushAsync();
//writer.WriteString(“go”+Environment.NewLine);
//分离流并关闭它
等待writer.FlushAsync();
writer.DetachStream();
writer.Dispose();
关闭=真;
clientSocket.Dispose();
clientSocket=null;
连接=错误;
}
捕获(异常)
{
//如果这是未知状态,
//这意味着错误是致命的,重试可能会失败。
if(Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult)==SocketErrorStatus.Unknown)
{
投掷;
}
StatusText.Text=“发送数据或接收失败,错误:”+异常。消息;
//无法重试连接,但对于这个简单的示例
//把插座合上。
关闭=真;
clientSocket.Dispose();
clientSocket=null;
连接=错误;
}
}
}
}
听众:

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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
Net系统;
使用System.Net.Sockets;
使用System.IO;
使用系统组件模型;
使用系统线程;
名称空间套接字侦听器
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
弦线;
公共主窗口()
{
初始化组件();
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();
        }
    }
}