0

これは非常に単純な問題だと思いますが、ここで少し問題があります。WPC フォーム + 別のクラスのコード全体を最後に貼り付けます。長くなって申し訳ありませんが、ISSUE という単語を検索すると、すぐに問題のある行にたどり着きます。

達成したいこと

-- 私のメインの WPF c# ウィンドウは MainWindow と呼ばれ、テキスト ボックス txtLog があります。

-- clientHandler というクラスの 1 つは、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 にアクセスできるようにしたいと考えています。

4

2 に答える 2

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 に答える
2

特にMVVMがサポートされ、推奨されている WPF では、フォームへの参照を渡すべきではありません。これはまさに、MVVM アーキテクチャが対処する種類の問題です。

UI 要素またはモデルのいずれかの変更が一度に 3 つの場所すべてに反映されるように、両方の UI 要素を同じモデルにバインドすることを検討してください。

于 2013-03-15T21:57:54.210 に答える