C# LoadData方法中的事件未触发

C# LoadData方法中的事件未触发,c#,wpf,xaml,windows-phone-8,windows-phone,C#,Wpf,Xaml,Windows Phone 8,Windows Phone,情况: 我在viewmodels中有两个类:MedicineModel和MedicineData 我在MedcineModel.cs中的LoadData方法末尾触发了一个事件Event1 然后在MainPage.xaml.cs中处理此事件,其中包含事件处理程序 指向我要执行的安全方法 问题: 问题是Event1没有启动 这是app.xaml.cs的代码 private static MedicineModel viewModel = null; public static Med

情况:

  • 我在viewmodels中有两个类:MedicineModel和MedicineData
  • 我在MedcineModel.cs中的LoadData方法末尾触发了一个事件Event1
  • 然后在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();
       }
   }
}
}

让我们看看你的应用程序的工作流程:

  • 您正在访问
    App.ViewModel
    属性,以检索ViewModel并将其分配给datacontext
  • App.ViewModel
    属性的getter中,实例化ViewModel,并调用
    LoadData
    方法
  • LoadData
    方法中,加载数据(显然),然后引发
    Event1
    事件
  • getter完成执行后,您将检索viewmodel的实例
  • 您可以订阅
    Event1
    事件

  • 按照这些步骤,无法在
    MainPage
    中引发
    Event1
    事件,因为您在事件被引发后订阅了

    我已在此处上传了工作测试项目-。这是一个基于您的类的WPF项目,但您可以轻松地从中获取代码并将其放入Windows Phone项目中。

    那么,如果我想在
    主页中引发
    Event1
    ,我该怎么办,因此,无论何时加载
    MainPage.xaml
    ,都会执行
    eventHandler
    。@user3442701创建类型的第二次引发事件有什么意义?可以直接调用要附加的方法,而不是附加处理程序。
    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;
        }