<tfoot id='gJq9F'></tfoot>
    <i id='gJq9F'><tr id='gJq9F'><dt id='gJq9F'><q id='gJq9F'><span id='gJq9F'><b id='gJq9F'><form id='gJq9F'><ins id='gJq9F'></ins><ul id='gJq9F'></ul><sub id='gJq9F'></sub></form><legend id='gJq9F'></legend><bdo id='gJq9F'><pre id='gJq9F'><center id='gJq9F'></center></pre></bdo></b><th id='gJq9F'></th></span></q></dt></tr></i><div id='gJq9F'><tfoot id='gJq9F'></tfoot><dl id='gJq9F'><fieldset id='gJq9F'></fieldset></dl></div>
  • <small id='gJq9F'></small><noframes id='gJq9F'>

    <legend id='gJq9F'><style id='gJq9F'><dir id='gJq9F'><q id='gJq9F'></q></dir></style></legend>
      <bdo id='gJq9F'></bdo><ul id='gJq9F'></ul>

      1. 用 C# 编写全局自定义事件

        Writing a global custom event in C#(用 C# 编写全局自定义事件)

            <i id='sx38q'><tr id='sx38q'><dt id='sx38q'><q id='sx38q'><span id='sx38q'><b id='sx38q'><form id='sx38q'><ins id='sx38q'></ins><ul id='sx38q'></ul><sub id='sx38q'></sub></form><legend id='sx38q'></legend><bdo id='sx38q'><pre id='sx38q'><center id='sx38q'></center></pre></bdo></b><th id='sx38q'></th></span></q></dt></tr></i><div id='sx38q'><tfoot id='sx38q'></tfoot><dl id='sx38q'><fieldset id='sx38q'></fieldset></dl></div>
            <tfoot id='sx38q'></tfoot><legend id='sx38q'><style id='sx38q'><dir id='sx38q'><q id='sx38q'></q></dir></style></legend>

            <small id='sx38q'></small><noframes id='sx38q'>

              <bdo id='sx38q'></bdo><ul id='sx38q'></ul>
                    <tbody id='sx38q'></tbody>
                • 本文介绍了用 C# 编写全局自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在这个表单上有一个 winform winform1 和 2 个用户控件 control1control2

                  I have a winform winform1 and 2 user controls control1 and control2 on this form

                  现在我想定义一个自定义事件,它在 control1 中引发/触发并在 control2 中接收.该事件应该是全局,而不是直接在 control1 中定义.control2 不应该知道 control1 的存在.该事件也应该由其他控件引发.C# 代码如何?我需要发布者类之类的东西吗?

                  Now I want to define a custom event, which is raised/fired in control1 and received in control2. The event should be global and not directly defined in control1. control2 should not know about the existence of control1. The event should also be raised by other controls. How is the C# code for that? Do I need something like a publisher class?

                  推荐答案

                  你描述的看起来像 Mediator模式,其中对象通过消息进行通信.这些消息可以实现为事件、回调或任何其他机制.

                  What you describe looks like the Mediator pattern, in which objects communicate through messages. These messages can be implemented as events, callbacks, or any other mechanism.

                  您可以使用 MVVM Light 的 Messenger 类之类的实现(此框架旨在与 WPF 和 Silverlight 一起使用,但您可以获取此特定类的代码并在 WinForms 中使用它)

                  You could use an implementation like MVVM Light's Messenger class (this framework is intended for use with WPF and Silverlight, but you can get the code for this particular class and use it in WinForms)

                  // Register for a specific message type
                  Messenger.Default.Register<TypeOfTheMessage>(this, DoSomething);
                  ...
                  
                  // Called when someone sends a message of type TypeOfTheMessage
                  private void DoSomething(TypeOfTheMessage message)
                  {
                      // ...
                  }
                  
                  // Send a message to all objects registered for this type of message
                  Messenger.Default.Send(new TypeOfTheMessage(...));
                  

                  Messenger 类相对于静态事件的一大优势是它使用弱引用,因此它不会阻止订阅对象的垃圾回收,从而降低内存泄漏的风险.

                  A big advantage of the Messenger class over a static event is that it uses weak references, so it doesn't prevent garbage collection of subscribed objects, which reduces the risk of memory leaks.

                  请参阅此链接了解有关Messenger

                  这篇关于用 C# 编写全局自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <small id='Q7d4M'></small><noframes id='Q7d4M'>

                  1. <i id='Q7d4M'><tr id='Q7d4M'><dt id='Q7d4M'><q id='Q7d4M'><span id='Q7d4M'><b id='Q7d4M'><form id='Q7d4M'><ins id='Q7d4M'></ins><ul id='Q7d4M'></ul><sub id='Q7d4M'></sub></form><legend id='Q7d4M'></legend><bdo id='Q7d4M'><pre id='Q7d4M'><center id='Q7d4M'></center></pre></bdo></b><th id='Q7d4M'></th></span></q></dt></tr></i><div id='Q7d4M'><tfoot id='Q7d4M'></tfoot><dl id='Q7d4M'><fieldset id='Q7d4M'></fieldset></dl></div>

                      <bdo id='Q7d4M'></bdo><ul id='Q7d4M'></ul>
                            <tbody id='Q7d4M'></tbody>
                          <legend id='Q7d4M'><style id='Q7d4M'><dir id='Q7d4M'><q id='Q7d4M'></q></dir></style></legend>

                            <tfoot id='Q7d4M'></tfoot>