为什么这个C#事件处理程序不响应此事件?

时间:2010-04-29 13:00:23

标签: c# wpf silverlight events event-handling

以下代码是一个silverlight应用程序,但在WPF中也是如此,所以它似乎只是我在委托,事件等方面缺少的东西。

任何人都可以告诉我为什么以下代码成功执行此事件:

OnLoadingComplete(this, null);

但从不执行此事件处理程序?

void initialDataLoader_OnLoadingComplete(object obj, DataLoaderArgs args)

CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Diagnostics;

namespace TestEvent22928
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            DataLoader initialDataLoader = new DataLoader("initial");
            initialDataLoader.RegisterText("test1", "http://test:111/testdata/test1.txt");
            initialDataLoader.RegisterText("test2", "http://test:111/testdata/test2.txt");
            initialDataLoader.BeginLoading();
            initialDataLoader.OnLoadingComplete += new DataLoader.LoadingComplete(initialDataLoader_OnLoadingComplete);
        }

        void initialDataLoader_OnLoadingComplete(object obj, DataLoaderArgs args)
        {
            Debug.WriteLine("loading complete"); //WHY DOES EXECUTION NEVER GET HERE?
        }
    }


    public class DataManager
    {
        public DataLoader CreateDataloader(string dataloaderIdCode)
        {
            DataLoader dataLoader = new DataLoader(dataloaderIdCode);
            return dataLoader;
        }
    }

    public class DataLoader
    {
        public string IdCode { get; set; }
        public List<DataItem> DataItems { get; set; }

        public delegate void LoadingComplete(object obj, DataLoaderArgs args);
        public event LoadingComplete OnLoadingComplete = delegate { };

        private int dataItemCurrentlyLoadingIndex;

        public DataLoader(string idCode)
        {
            IdCode = idCode;
            DataItems = new List<DataItem>();
            dataItemCurrentlyLoadingIndex = -1;
        }

        public void RegisterText(string idCode, string absoluteSourceUrl)
        {
            DataItem dataItem = new DataItem
            {
                IdCode = idCode,
                AbsoluteSourceUrl = absoluteSourceUrl,
                Kind = DataItemKind.Text
            };
            DataItems.Add(dataItem);
        }

        public void BeginLoading()
        {
            LoadNext();
        }

        private void LoadNext()
        {
            dataItemCurrentlyLoadingIndex++;
            if (dataItemCurrentlyLoadingIndex < DataItems.Count())
            {
                DataItem dataItem = DataItems[dataItemCurrentlyLoadingIndex];
                Debug.WriteLine("loading " + dataItem.IdCode + "...");
                LoadNext();
            }
            else
            {
                OnLoadingComplete(this, null); //EXECUTION GETS HERE
            }
        }
    }

    public class DataItem
    {
        public string IdCode { get; set; }
        public string AbsoluteSourceUrl { get; set; }
        public DataItemKind Kind { get; set; }
        public object DataObject { get; set; }
    }

    public enum DataItemKind
    {
        Text,
        Image
    }

    public class DataLoaderArgs : EventArgs
    {
        public string Message { get; set; }
        public DataItem DataItem { get; set; }

        public DataLoaderArgs(string message, DataItem dataItem)
        {
            Message = message;
            DataItem = dataItem;
        }
    }
}

1 个答案:

答案 0 :(得分:7)

您在开始加载后只注册处理程序:

        initialDataLoader.BeginLoading();
        initialDataLoader.OnLoadingComplete += new DataLoader.LoadingComplete(initialDataLoader_OnLoadingComplete);

当前编写代码的方式,看起来像BeginLoading()阻塞直到完成,这意味着永远不会调用处理程序,因为直到完成加载后才设置它。