问题描述
我有一个 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;
// ...
}
// ...
}
这篇关于事件触发次数越来越多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!