简体   繁体   English

C#表格沟通

[英]c# forms communication

I have a C# application which consists of 3 forms: 我有一个包含3种形式的C#应用​​程序:

 1: Battleship Game GUI 2: Network GUI (does client/server connections) 3: Chat GUI 

Form 1 is loaded first. 首先加载表格1。 When the user selects setup network , Form 2 is displayed. 当用户选择setup network ,将显示表格2。

Chats are displayed when the user opts to send a chat or when a chat is received. 当用户选择发送聊天或接收到聊天时,将显示聊天。

I would like Form 2 to process all the messages and pass on the relevant messages to the relevant GUI to decode the message further. 我希望Form 2处理所有消息并将相关消息传递给相关GUI,以进一步对消息进行解码。

I'm still at an early stage of development. 我仍处于开发初期。 At the moment I am trying to use delegates to communicate between the forms . 目前,我正在尝试使用委托在表单之间进行通信

Is this the best way of doing this? 这是最好的方法吗? What are the best practices regarding components of an application sending messages to each other? 关于应用程序组件之间相互发送消息的最佳实践是什么?

ikurts, this is definitely not good practice. ikurts,这绝对不是一个好习惯。 Your UIs, however many there are, should have absolutely nothing to do with communications. 您的UI,无论有多少,都应该与通信完全无关。 You should: 你应该:

  • read up on model/view/controller in .Net to see a good way of structuring a project like yours 在.Net中阅读模型/视图/控制器,以了解构建类似您的项目的好方法
  • read up on using threading for your communications, ought to be simple 阅读有关使用线程进行通信的内容,应该很简单

It sounds like you want to follow best practices in setting up your app. 听起来您想遵循最佳做法来设置您的应用。 You're gonna get a lot of feedback on this question i think and all of it will sound something like this. 我想您将在这个问题上得到很多反馈,所有这些听起来都会像这样。 Do yourself a favor and follow it. 帮自己一个忙,并遵循它。 And stay away from comm logic in forms!!! 并远离表单中的通信逻辑!!!

A few more thoughts: 还有一些想法:

i'm not sure why you want to break this up into three screens when two seem logical: 1) network settings dialog 2) game play dialog that could accommodate both the battleship UI and your chat UI. 我不确定为什么要在两个看起来合乎逻辑的情况下将其分为三个屏幕:1)网络设置对话框2)可以同时容纳战舰UI和聊天UI的游戏对话框。 This configuration would also simplify your structure since only one screen, the gameplay/chat screen would need to be updated. 这种配置还可以简化您的结构,因为只需要更新一个屏幕,即游戏/聊天屏幕。 The settings dialog would be just that. 设置对话框就是这样。

Here is an example of a threaded chat application on CodeProject which would server well as a code base to get started. 这是CodeProject上的线程聊天应用程序的示例,该应用程序将很好地用作服务器代码库。 i imagine that your battleship moves would simply be "special" chat messages that specify board hits. 我想象您的战舰移动只是指定板命中的“特殊”聊天消息。

Also, here's an example of a network enabled tic tac toe game that may yield clues as to developing a client/sever game. 另外,这是一个基于网络的井字游戏的示例,该游戏可能会提供有关开发客户端/服务器游戏的线索。

Don't shy away from what seems difficult! 不要回避似乎困难的地方! My suggestion is to first write a chat/communication client which has no UI, perhaps just console output. 我的建议是首先编写一个没有UI的聊天/通讯客户端,也许只是控制台输出。 Then add a screen and you'll see that you won't have to marry the window to the network stuff. 然后添加一个屏幕,您将看到不必将窗口与网络内容关联。

Good luck! 祝好运!

More links: 更多链接:

Here's a nice discussion about MVC/MVP that may enlighten your architecture. 这是有关MVC / MVP的精彩讨论 ,可能会启发您的体系结构。 And here's another ... From a different angle. 还有另一个 ……从另一个角度来看。

You should separate the comms into a different class (not in a Form). 您应该将逗号分隔为不同的类(而不是在窗体中)。

However, to keep your forms "in sync" with each other, you can use c# events (and delegates to handle them) to inform one form that something has happened on another form. 但是,为了使表单彼此“同步”,可以使用c#事件(和委托来处理它们)来通知一种表单另一种表单已经发生了事情。 You're using events to handle all the form actions (mouse button clicks etc) already, so you should have a basic idea of how it works, and there are lots of articles on the net about "c# events" and "c# event handling" (there are some search terms for you) that will give you more info, so I won't go into detail here. 您已经在使用事件来处理所有表单操作(鼠标单击等),因此您应该对它的工作方式有一个基本的了解,并且网上有很多关于“ c#事件”和“ c#事件处理”的文章。 ”(为您提供一些搜索字词),它将为您提供更多信息,因此在此不再赘述。 The advantage of events is that the system remains loosely coupled, and anybody can subscribe to them, so it's really easy for you to add a fourth form in the future and get it to "listen" to the information it needs. 事件的优点是系统保持松散耦合,任何人都可以订阅它们,因此将来添加第四种表格并使其“收听”所需的信息确实非常容易。

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

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