简体   繁体   English

如何将套接字作为参数传递给ButtonClick处理函数?

[英]How can I pass socket as an argument do ButtonClick handler function?

I need to pass my Socket variable to function that handles clicking of a button. 我需要将Socket变量传递给处理按钮单击的函数。 It's a Tic-Tac-Toe game that I'm trying to make that it sends the outcome of the game to server. 这是我要制作的Tic-Tac-Toe游戏,它将游戏的结果发送到服务器。 In my ButtonClick() (which is used for each of 9 cells), I have a function that checks if someone won, and if yes it sould send the outcome to the server. 在我的ButtonClick()(用于9个单元格中的每个单元)中,我有一个函数来检查是否有人赢了,如果可以,它将结果发送到服务器。

I've tried starting the client every win win the check fynction but i just freezes the program and does nothing. 我已经尝试启动客户端,每次胜利都将赢得支票功能,但我只是冻结程序,什么也不做。 I've come up an Idea that i can start the client in the constructor and then in my check function only send the info about winner. 我提出了一个想法,我可以在构造函数中启动客户端,然后在我的检查功能中仅发送有关获胜者的信息。 So the problem emerges, and I can't pass my socket through the event handler function because it only accepts two arguments. 因此问题出现了,我无法通过事件处理函数传递套接字,因为它仅接受两个参数。

These are button definitions: 这些是按钮定义:

<Button Grid.Column="0" Grid.Row="0" x:Name="Button0_0" Click="ButtonClick" />
        <Button Grid.Column="1" Grid.Row="0" x:Name="Button1_0" Click="ButtonClick" />
        <Button Grid.Column="2" Grid.Row="0" x:Name="Button2_0" Click="ButtonClick" />
        <Button Grid.Column="0" Grid.Row="1" x:Name="Button0_1" Click="ButtonClick" />
        <Button Grid.Column="1" Grid.Row="1" x:Name="Button1_1" Click="ButtonClick" />
        <Button Grid.Column="2" Grid.Row="1" x:Name="Button2_1" Click="ButtonClick" />
        <Button Grid.Column="0" Grid.Row="2" x:Name="Button0_2" Click="ButtonClick" />
        <Button Grid.Column="1" Grid.Row="2" x:Name="Button1_2" Click="ButtonClick" />
        <Button Grid.Column="2" Grid.Row="2" x:Name="Button2_2" Click="ButtonClick" />

This is my button handler: 这是我的按钮处理程序:

 private void ButtonClick(object sender1, RoutedEventArgs e)
        {
            if (mGameEnded) {
                NewGame();
                return;
            }
            //operations
            CheckWinner();
        }

And here is the necessary part of my check funtion: 这是我的检查功能的必要部分:

 private void CheckWinner()
        {
            //checking operations

            if (mGameEnded)
            {
                MessageBox.Show(winner, "Who won?");

                // Encode the data string into a byte array.    
                byte[] msg = Encoding.ASCII.GetBytes(winner);

                // Send the data through the socket.    
                sender.Send(msg);

            }

        }

As you can see, I'd need to pass the socket through the ButtonClick() to the CheckWinner() to use the SendToServer() function. 如您所见,我需要将套接字通过ButtonClick()传递给CheckWinner()才能使用SendToServer()函数。

This isn't best practice, but... 这不是最佳做法,但是...

Add another private method socketSend to your window. 向窗口添加另一个私有方法socketSend。

That takes parameters like the msg. 这需要像味精这样的参数。

That can instantiate a sockkect, send the msg and close the connection. 这样可以实例化一个ockock,发送消息并关闭连接。

Similar to ExecuteClient in this: 与ExecuteClient类似:

https://www.geeksforgeeks.org/socket-programming-in-c-sharp/ https://www.geeksforgeeks.org/socket-programming-in-c-sharp/

Since all your methods are private members of the window, you could just make your socket another private member and ButtonClick could reference it. 由于您的所有方法都是窗口的私有成员,因此您只需将套接字设为另一个私有成员,ButtonClick便可以引用它。

If you're learning wpf for professional use you might be wondering about best practice. 如果您正在学习用于专业用途的WPF,您可能会想知道最佳实践。

Look into MVVM and break out your functionality into separate classes. 研究MVVM并将您的功能分成单独的类。 Then look into dependency injection. 然后研究依赖注入。 All that is some time down the road if you're a beginner though. 如果您是初学者,那么所有这一切都需要一定的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM