事件触发越来越多的时间 [英] Event fires more and more times
问题描述
我必须加载与加载到2 ContentControls,其中一个列表框显示项目和其他与编辑按钮2用户控件主视图中的Silverlight应用程序MVVM。当我点击编辑按钮,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的数据,所以我已经把它的构造函数的视图模型:
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();
这不会从活动中删除实例的,但创建一个新的实例 - 实例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屋!