【问题标题】:An event in LoadData method not firingLoadData 方法中的事件未触发
【发布时间】: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


    【解决方案1】:

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

    1. 您正在访问 App.ViewModel 属性,以检索视图模型并将其分配给您的数据上下文
    2. App.ViewModel 属性的getter 中,实例化视图模型,并调用LoadData 方法
    3. LoadData 方法中,您加载数据(显然),然后引发Event1 事件
    4. getter 完成执行,您检索视图模型实例
    5. 您订阅了Event1 事件

    按照这些步骤,Event1 事件将无法在 MainPage 中引发,因为您是在 引发之后订阅的。

    【讨论】:

    • 那么如果我想在MainPage 中提高Event1 怎么办,这样eventHandler 就会在MainPage.xaml 加载时执行?
    • @user3442701 在创建类型的第二个事件就会引发事件有什么意义?可以直接调用将要附加的方法,而不是附加处理程序。
    【解决方案2】:

    我已经在这里上传了工作测试项目 - http://1drv.ms/NOvNSd。这是一个基于您的类的 WPF 项目,但您可以轻松地从中获取代码并将其放入您的 Windows Phone 项目中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2010-11-08
      • 2019-01-31
      相关资源
      最近更新 更多