开发者

Updating GUI from a different class?

开发者 https://www.devze.com 2023-01-09 15:47 出处:网络
Perhaps this is a very basic question, please pardon me if it is --- I\'m a beginner in C#. I have a WPF GUI for sending commands to a receiver. The commands are ASCII texts. Now, I separated the com

Perhaps this is a very basic question, please pardon me if it is --- I'm a beginner in C#.

I have a WPF GUI for sending commands to a receiver. The commands are ASCII texts. Now, I separated the communication part into a separate project because I have console applications that use the communication methods in addition to the GUI. The communication DLL uses async methods on normal sockets.

Now the problem is that I will have a circular dependency. The GUI needs to call communication routines to send messages, and 开发者_JAVA技巧the routines need to call GUI to show the responses. I understand that I could make an interface which the communication inherits and get around the dependency, but is this the best way to do this? Or is this a flaw in my design? I basically wanted to decouple the GUI from the underlying communication layer.


Your library (dll) shouldn't update the GUI directly. You should have it raise events. Ideally the events would be at a higher level of abstraction as well ("Message Received Successfully" rather than "Packet Received Successfully", but it's really up to you how much of the low level functionality of the library you want to expose)

The GUI (or console app or a completely different library) can now subscribe to the events and access the event data. The GUI, as a higher layer knows about the library, but the library shouldn't know about the GUI (or the console appliation)

0

精彩评论

暂无评论...
验证码 换一张
取 消