我确定这是一个非常简单的问题,但我在这里遇到了一些问题。我会将整个代码粘贴到WPC表单+另一个类的末尾,对不起它很长但只是搜索单词ISSUE并且你会直接解决问题
我想要实现的目标
- 我的主要WPF c#窗口名为MainWindow,有一个文本框txtLog
- 我的一个名为clientHandler的类需要更改txtLog中的值
- 目前我将MainWindow类的引用从MainWindow窗口传递给MainWindowHandle方法。
长代码 - 请在我的ClientHandler类中搜索问题行的单词ISSUE
主窗口
public partial class ServerWindow : Window
{
public static int NoOfRunningClients;
public static ClientHandler[] RunningClientsPool = new ClientHandler[50];
public static ClientHandler test;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public ServerWindow()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
NoOfRunningClients = 0;
//RunningClientsPool[NoOfRunningClients];
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
RunningClientsPool[NoOfRunningClients] = new ClientHandler();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(RunningClientsPool[NoOfRunningClients].AcceptCallback),
listener);
RunningClientsPool[NoOfRunningClients].MainWindowHandle = this;
// Wait until a connection is made before continuing.
allDone.WaitOne();
NoOfRunningClients++;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
}
}
ClientHandler的
public class ClientHandler
{
private Socket clientSocket;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public ClientHandler(Socket newSocket)
{
this.clientSocket = newSocket;
}
public ClientHandler()
{
}
public String MyName { get; set; }
public Window MainWindowHandle { get; set; }
public void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
// ISSUE: I want to use something like this
//but have the static reference problem
//MainWindowHandle.txtLog.Text = "It WORKS!!!!!";
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content);
// Echo the data back to the client.
Send(handler, content);
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
private void Send(Socket handler, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
非常感谢您的时间,重复一遍,我希望能够访问MainWindow和其他GUI元素中的txtLog,而不会获得持续的静态成员/非静态成员问题。
答案 0 :(得分:2)
特别是对于支持和鼓励MVVM的WPF,您不应该传递对表单的引用。这正是MVVM架构所解决的问题。
查看binding both UI elements to the same model,以便UI元素或模型中的更改同时反映在所有三个位置。
答案 1 :(得分:2)
不确定这是否有效但我会尝试在类'ClientHandler'中创建一个属性,并将其设置为冒泡到其他视图等。像
这样的东西public string response { get; set; }
...
// ISSUE: I want to use something like this
//but have the static reference problem
//MainWi
response = "It WORKS!!!!!"; // set the response PROPERTY to be set
然后,只要类是公共的,就可以在实例化时查看并设置其他对象。
ClientHandler ch = new ClientHandler();
... (perform operations on class that invokes method)
TextBox.Text = ch.response // as long as your event or method is performed that updates the property it will be set.
老实说虽然我同意Eric J. MVVM在这方面要好得多。它隔离事件,方法和属性,并将它们分隔在ViewModel中,然后绑定到您需要的内容。这是一个优点,因为您可以嵌套属性并选择它们的绑定方式。最初设置起来比较困难,但是从长远来看,当有多个事情相互交谈时,这样做会更好。