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

      1. <small id='dVwwM'></small><noframes id='dVwwM'>

        事件触发次数越来越多

        时间:2023-09-15
              • <bdo id='O4wfc'></bdo><ul id='O4wfc'></ul>

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

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

                  本文介绍了事件触发次数越来越多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 silverlight mvvm 应用程序,它加载主视图,其中 2 个用户控件加载到 2 个 ContentControls 中,一个带有显示项目的列表框,另一个带有编辑按钮.当我单击编辑按钮时,2 个新的用户控件加载到 ContentControls 中,一个显示要编辑的数据(EditData),另一个显示保存和取消按钮(EditAction).当我单击保存按钮时,它会引发一个在单独的 GlobalEvents.cs 类中定义的事件,例如:

                  I have a silverlight mvvm application that loads main view with 2 user controls loaded into 2 ContentControls, one with listbox showing items and other with edit button. When i click edit button, 2 new user controls load into the ContentControls, one showing data to edit (EditData) and other having Save and Cancel button (EditAction). When i click save button, it raises an event that is defined in seperate GlobalEvents.cs class like:

                  public event EventHandler OnSaveButtonClicked;  
                  public void RaiseSaveButtonClicked()  
                  {  
                    this.OnSaveButtonClicked(this, EventArgs.Empty);  
                  }
                  

                  我在另一个用户控件 EditData 中订阅它,因为我需要通过自定义 EventArgs 传输编辑过的数据,所以我已经放入了它的 ViewModel 的构造函数:

                  and i subscribe to it in the other user control EditData, because i need to transfer that edited data via custom EventArgs, so i have put in the constructor of it's ViewModel:

                  this.globalEvents.OnSaveButtonClicked += (s, e) => SaveData();  
                  

                  并在保存数据中:

                  public void SaveData()  
                  {  
                      globalEvents.RaiseSaveData(EditedGuy);     
                  }  
                  

                  引发另一个事件,将先前的用户控件加载到其 ControlContent 中并在列表框中显示编辑的数据.没关系,但是每当我单击编辑然后再次保存时,它会引发事件两次,再次引发 3 次,然后是 4 次,依此类推.我怎样才能让它只被提升一次?我认为这可能是因为每次单击编辑时,都会加载用户控件的新实例,但我不知道,也许对事件的订阅仍然存在,所以我尝试粘贴

                  which raises another event that loads previous user controls into their ControlContent and shows edited data in list box. Thats all fine, but whenever i click on edit and then save again, it raises the event twice, and again 3 times, then 4 and so on. How can i make it to be raised only ONE time? I thought it could be because every time i click edit, a new instance of the user control is loaded and i dont know, maybe the subscription to the event stays, so i have tried to paste

                  this.globalEvents.OnSaveButtonClicked -= (s, e) => SaveData(); 
                  

                  到 Dispose() 方法但没有成功.我怎样才能做到这一点?

                  to the Dispose() method but without success. How can i make this work?

                  推荐答案

                  当你想从事件中注销时,你不能使用 lambdas.

                  You can't use lambdas when you want to unregister from events.

                  this.globalEvents.OnSaveButtonClicked += (s, e) => SaveData(); 
                  

                  这将创建一个 EventHandler 类型的实例 - 我们称之为实例 A - 并将其添加为处理程序.

                  This will create one instance - let's call it instance A - of type EventHandler and add it as a handler.

                  this.globalEvents.OnSaveButtonClicked -= (s, e) => SaveData(); 
                  

                  这不会从事件中移除实例 A,而是创建一个新实例 - 实例 B - 并尝试将其从事件中移除.

                  This will not remove instance A from the event but create a new instance - instance B - and tries to remove it from the event.

                  要解决此问题,请创建一个小方法或将该匿名方法保存在字段中:

                  To fix this problem, either create a little method or save that anonymous method in a field:

                  class ViewModel
                  {
                  
                      private EventHandler _saveButtonClickedHandler;
                      // ...
                  
                      public ViewModel()
                      {
                          _saveButtonClickedHandler = (s, e) => SaveData();
                          this.globalEvents.OnSaveButtonClicked += _saveButtonClickedHandler;
                          // ...
                      }
                  
                      public void Dispose()
                      {
                          this.globalEvents.OnSaveButtonClicked -= _saveButtonClickedHandler;
                          // ...
                      }
                  
                      // ...
                  }
                  

                  这篇关于事件触发次数越来越多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检测表单何时关闭c# 下一篇:在字符串中动态添加具有委托名称的事件处理程序

                  相关文章

                  <small id='0ILgv'></small><noframes id='0ILgv'>

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

                    <legend id='0ILgv'><style id='0ILgv'><dir id='0ILgv'><q id='0ILgv'></q></dir></style></legend>
                    1. <tfoot id='0ILgv'></tfoot>