在另一个事件中创建通知已完成的事件

时间:2014-03-06 05:46:33

标签: c# windows-phone-8

我有mainviewmodel,它创建itemviewmodel的集合,并将该数据绑定到Mainpage.xaml中的longlist。现在在制作ItemViewModel的集合的过程中,我正在制作一个Web请求,当下载时我会列出一个列表。

我想在MainPage中知道这个下载结束时。

MainViewModel

public void LoadData()
    {
        if (this.CanLoad)
        {
            WebClient dealsOfDay = new WebClient();
            dealsOfDay.DownloadStringCompleted += dealsOfDay_DownloadStringCompleted;
            dealsOfDay.DownloadStringAsync(new Uri("http://loadsomedata.php"));
        }
        else
        {
            this.IsDataLoaded = false;
        }
    }

void dealsOfDay_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null && e.Result != null)
        {

            var deals=//something making  a collection.

            Items = new ObservableCollection<ItemViewModel>(deals);
            NotifyPropertyChanged("Items");

            this.IsDataLoaded = true;
        }
        else
        {
            MessageBox.Show("Error");
        }
    }

App.xaml.cs

private static MainViewModel viewModel = null;

    /// <summary>
    /// A static ViewModel used by the views to bind against.
    /// </summary>
    /// <returns>The MainViewModel object.</returns>
    public static MainViewModel ViewModel
    {
        get
        {
            // Delay creation of the view model until necessary
            if (viewModel == null)
                viewModel = new MainViewModel();

            return viewModel;
        }
    }

MainPage.xaml.cs中 在构造函数内部,我会设置它。

DataContext = App.ViewModel;
if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }

2 个答案:

答案 0 :(得分:1)

我认为您不需要bool IsDataLoaded。而是在MainViewModel中创建事件并在主页面中注册。

public event EventHandler DataLoadedEvent;
void dealsOfDay_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null && e.Result != null)
    {

        var deals=//something making  a collection.

        Items = new ObservableCollection<ItemViewModel>(deals);
        NotifyPropertyChanged("Items");

        if ( DataLoadedEvent != null)
        {
            DataLoadedEvent(this, new EventHandler());
        }
    }
    else
    {
        MessageBox.Show("Error");
    }
}

现在在MainPage构造函数中注册此事件。

App.ViewModel.DataLoadedEvent += new EventHandler(data_loadedEvent);


void data_loadedEvent(object sender, EventArgs e)
{
     App.ViewModel.LoadData();
}

答案 1 :(得分:1)

还要记住,你可以订阅更多方法DownloadStringCompleted - 并且它们会被解雇,所以也许不需要创建新事件。此外,在许多情况下,您只需在dealsOfDay_DownloadStringCompleted中执行操作即可。

但是如果你想创建一个在DownloadCompletes时会被触发的事件,它可能如下所示:

创建委托:

public delegate void StatusUpdateHandler(object sender, StatusEventArgs e);
public event StatusUpdateHandler OnUpdateStatus;

为此,您需要在某处定义StatusEventArgs类:

public class StatusEventArgs : EventArgs
{
    public string Status { get; private set; }

    public StatusEventArgs(string status)
    {
        Status = status;
    }
}

然后您的方法可能如下所示:

private void UpdateStatus(string status)
{
   if (OnUpdateStatus == null) return;

   StatusEventArgs args = new StatusEventArgs(status);
   OnUpdateStatus(this, args);
}

然后您可以自由订阅该活动并加入dealsOfDay_DownloadStringCompleted

UpdateStatus("Downloaded");