【发布时间】:2014-03-25 10:41:01
【问题描述】:
情况:
- 我在视图模型中有两个类:MedicineModel 和 MedicineData。
- 我有一个事件 Event1,我在位于 MedcineModel.cs 的 LoadData 方法结束时触发。
- 然后在 MainPage.xaml.cs 中处理此事件,其中事件处理程序 指向我要执行的具体方法。
问题:
- 问题是 Event1 没有触发。
这是 app.xaml.cs 的代码
private static MedicineModel viewModel = null;
public static MedicineModel ViewModel {
get
{
if (viewModel==null)
{
viewModel = new MedicineModel();
viewModel.LoadData();
}
return viewModel;
}
}
MedicineModel.cs 的代码是
namespace MedicinePlus.ViewModels
{
public delegate void EventDelegate();
public class MedicineModel
{
public List<MedicineData> Problems { get; set; }
public MedicineModel()
{
this.Problems = new List<MedicineData>();
}
public bool IsDataLoaded { get; set; }
public event EventDelegate Event1;
public void LoadData()
{
//place rt time Data here
IsDataLoaded = true;
this.Problems.Add(new MedicineData() { ID = 0, ProblemName = "Fever"});
this.Problems.Add(new MedicineData(){ID=1,ProblemName="Diarrhea"});
this.Problems.Add(new MedicineData() { ID=2,ProblemName = "sprain"});
this.Problems.Add(new MedicineData() { ID = 3, ProblemName = "bruise" });
OnEvent1();
}
protected virtual void OnEvent1()
{
EventDelegate handler = Event1;
if (handler!=null)
{
handler();
}
}
}
}
而MainPage.xaml.cs的代码是
其中 textListViewSource 是集合视图源的名称,listBoxTextItems 是列表框的名称
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
App.ViewModel.Event1 += new EventDelegate(eventHandler);
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void eventHandler()
{
textListViewSource.Source = App.ViewModel.Problems;
listBoxTextItems.DataContext = App.ViewModel.Problems;
}
【问题讨论】:
标签: c# wpf xaml windows-phone-8 windows-phone