大家好我正在尝试通过 TCP/IP 读取值来动态更新文本块。我做错了什么,我该如何解决?另外,如果有更好的做事方式,我愿意学习。
这里我使用一个public static来在其他类中使用这个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Script_Launcher.Validation
{
//Global variable to read message elsewhere in the code.
public class Message
{
private static string message = "";
public static string Messager
{
get { return message; }
set { message = value; }
}
}
}
我在这里打开一个 exe 作为客户端并将数据发送到我的服务器。
using HandyControl.Data;
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Script_Launcher.Validation;
using System.Threading.Tasks;
namespace Script_Launcher.Views
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
TcpListener server;
TcpClient clientconnection;
private void ListBoxItem_Selected_1(object sender, RoutedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("OSP_Analysis"))
{
process.Kill();
}
Process.Start(@"C:\Users\path\OSP_Analysis");
Task.Run(() =>
{
//Vars
//TcpListener server;
//TcpClient clientconnection;
server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8088); //Opens Server on said port
server.Start(); //Starts listener
Trace.WriteLine("Server started...");
clientconnection = server.AcceptTcpClient(); //Accepts Request
NetworkStream DataStream = clientconnection.GetStream(); //Gets data stream
byte[] buffer = new byte[clientconnection.ReceiveBufferSize];
int Data = DataStream.Read(buffer, 0, clientconnection.ReceiveBufferSize); //Recieves data (encoded)
Message.Messager = Encoding.ASCII.GetString(buffer, 0, Data); //Decodes data (python .encode() function uses the ASCII code)
clientconnection.Close();
server.Server.Close();
});
}
}
}
尝试使用比较来更新文本块
using System.Net.Sockets;
using System.Net;
using System.Text;
using System;
using System.Windows.Controls;
using System.Diagnostics;
using System.Threading;
using System.Drawing.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
using static Script_Launcher.Views.OilPerformance;
using System.Runtime.Remoting.Messaging;
using System.Windows.Media;
using Script_Launcher.Validation;
namespace Script_Launcher.Views
{
/// <summary>
/// Interaction logic for OilPerformance
/// </summary>
public partial class OilPerformance : UserControl
{
public OilPerformance()
{
InitializeComponent();
string message = Message.Messager;
Trace.WriteLine(message);
if (message == "1500")
{
PASSFAIL.Text = "PASS!";
PASSFAIL.Foreground = new SolidColorBrush(Colors.Green);
}
else if (message == "1501")
{
PASSFAIL.Text = "FAIL!";
PASSFAIL.Foreground = new SolidColorBrush(Colors.Red);
}
else
{
PASSFAIL.Text = "WAITING...";
PASSFAIL.Foreground = new SolidColorBrush(Colors.Orange);
}
}
}
}
最后是 XAML
<UserControl x:Class="Script_Launcher.Views.OilPerformance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:hc="https://handyorg.github.io/handycontrol"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<Grid Margin="20" Background="{DynamicResource RegionBrush}">
</Grid>
<Grid Margin="40" VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Margin="32,64,32,32" VerticalAlignment="Center">
<TextBlock x:Name="PASSFAIL" Margin="0,15,0,-15" FontSize="72"></TextBlock>
</StackPanel>
</Grid>
</Grid>
</UserControl>