イベントに関して3つの質問があります。
- 購読したイベントは常に購読を解除する必要がありますか?
- そうしないとどうなりますか?
- 以下の例では、サブスクライブされたイベントからどのようにサブスクライブを解除しますか?
たとえば、次のコードがあります。
コンストラクター:目的:データベースプロパティの更新用
this.PropertyChanged += (o, e) =>
{
switch (e.PropertyName)
{
case "FirstName": break;
case "LastName": break;
}
};
そしてこれ:目的:GUIバインディングの場合、モデルをビューモデルにラップします
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);
}