事件触发的次数越来越多 [英] Event fires more and more times

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

问题描述

我有一个 Silverlight mvvm 应用程序,它加载主视图,其中 2 个用户控件加载到 2 个内容控件中,一个带有显示项目的列表框,另一个带有编辑按钮.当我单击编辑按钮时,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?

推荐答案

当您想从事件中取消注册时,不能使用 lambda.

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

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

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

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;
        // ...
    }

    // ...
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆