0

我确定这是一个非常简单的问题,但我在这里遇到了一些问题。我将在最后粘贴 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();


    }

        }






    }

客户端处理程序

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 中的 txtLog 和其他 GUI 元素,而不会遇到恒定的静态成员/非静态成员问题。

4

2 回答 2

2

特别是在支持和鼓励MVVM的WPF 中,您不应该传递对表单的引用。这正是 MVVM 架构解决的问题。

研究将两个 UI 元素绑定到同一个模型,以便 UI 元素或模型中的更改同时反映在所有三个位置。

于 2013-03-15T21:57:54.210 回答
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 中,然后您将其绑定到您需要的内容。这是一个优势,因为您可以嵌套属性并选择它们的绑定方式。最初设置起来比较困难,但是从长远来看,当多个事物相互交谈时,使用起来会更好。

于 2013-03-15T22:09:23.683 回答