我应该取消的事​​件? [英] Should I unsubscribe from events?

查看:127
本文介绍了我应该取消的事​​件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个关于事件的问题:


  1. 我应该永远被取消订阅的事件?

  2. 如果我不会怎么样?

  3. 在下面的例子中,你将如何从订阅的事件退订?

我有这个例子code:

构造函数:用途:用于数据库属性更新

  this.PropertyChanged + =(O,E)=>
{
    开关(e.PropertyName)
    {
        案名字:打破;
        案名字:打破;
    }
};

和这样的:用途:用于GUI的裹包模型成的ViewModels

 的ObservableCollection<期及GT;周期= _l prepo.GetDailyLessonPlanner(data.DailyDate);
PeriodListViewModel =新的ObservableCollection< PeriodViewModel>();的foreach(在时间周期期间)
{
    PeriodViewModel periodViewModel =新PeriodViewModel(期间,_l prePO);
    的foreach(DocumentListViewModel documentListViewModel在periodViewModel.DocumentViewModelList)
    {
        documentListViewModel.DeleteDocumentDelegate + =新动作<列表与lt;文件>>(OnDeleteDocument);
        documentListViewModel.AddDocumentDelegate + =新动作(OnAddDocument);
        documentListViewModel.OpenDocumentDelegate + =新动作< INT,串>(OnOpenDocument);
    }
    PeriodListViewModel.Add(periodViewModel);
}


解决方案

1),这要看情况。通常这是一个好主意,但也有在这里你不需要典型案例。基本上,如果你确信订阅对象将活得比事件源,你应该取消,否则这会造成不必要的参考。

如果但是您的目的是在以下订阅其自己的事件,如:

 <窗​​口已加载=self_Loaded...> ...< /窗GT;

- 那么你不必

2)订阅事件使得更多的参考订阅对象。所以,如果你不退订,你的对象可能由该参考保持活力,从而有效的内存泄漏。通过退订要删除该引用。注意,在自订阅的情况下,问题就不会出现。

3)你可以做这样的:

  this.PropertyChanged + = PropertyChangedHandler;
...
this.PropertyChanged - = PropertyChangedHandler;

其中,

 无效PropertyChangedHandler(对象o,PropertyChangedEventArgs E)
{
    开关(e.PropertyName)
    {
        案名字:打破;
        案名字:打破;
    }
}

I have 3 questions concerning events:

  1. Should I always unsubscribe events that were subscribed?
  2. What happens if I do NOT?
  3. In the below examples, how would you unsubscribe from the subscribed events?

I have for example this code:

Ctor: Purpose: For database property updates

this.PropertyChanged += (o, e) =>
{
    switch (e.PropertyName)
    {
        case "FirstName": break;
        case "LastName": break;
    }
};

and this: Purpose: For GUI-binding wrap the model into viewmodels

ObservableCollection<Period> periods = _lpRepo.GetDailyLessonPlanner(data.DailyDate);
PeriodListViewModel = new ObservableCollection<PeriodViewModel>();

foreach (Period period in periods)
{
    PeriodViewModel periodViewModel = new PeriodViewModel(period,_lpRepo);
    foreach (DocumentListViewModel documentListViewModel in periodViewModel.DocumentViewModelList)
    {
        documentListViewModel.DeleteDocumentDelegate += new Action<List<Document>>(OnDeleteDocument);
        documentListViewModel.AddDocumentDelegate += new Action(OnAddDocument);
        documentListViewModel.OpenDocumentDelegate += new Action<int, string>(OnOpenDocument);
    }
    PeriodListViewModel.Add(periodViewModel);
}

解决方案

1) It depends. Usually it's a good idea, but there are typical cases where you don't need to. Basically, if you are sure that the subscribing object is going to outlive the event source, you ought to unsubscribe, otherwise this would create an unnecessary reference.

If however your object is subscribing to its own events, like in the following:

<Window Loaded="self_Loaded" ...>...</Window>

--then you don't have to.

2) Subscribing to an event makes additional reference to the subscribing object. So if you don't unsubscribe, your object might be kept alive by this reference, making effectively a memory leak. By unsubscribing you are removing that reference. Note that in the case of self-subscription the problem doesn't arise.

3) You can do like that:

this.PropertyChanged += PropertyChangedHandler;
...
this.PropertyChanged -= PropertyChangedHandler;

where

void PropertyChangedHandler(object o, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "FirstName": break;
        case "LastName": break;
    }
}

这篇关于我应该取消的事​​件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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