加载页面后的加载方法

时间:2014-06-08 22:56:47

标签: c# windows-phone-8 windows-phone

我有一种下载XML并将其数据分类到collectionA的方法。然后,我collectionA分成两个较小的集合,collectionB&填充2个列表的collectionC。现在我遇到的问题是填充和拆分collectionA的方法是在collectionA有时间填充自己之前完成的。

我如何绕过collectionB& collectionC等待collectionA填充?

填充collectionA

的方法
    public void downloadXML(bool data)
            {
                if (data == false)
                {
                    WebClient wc = new WebClient();
                    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                    wc.DownloadStringAsync(new Uri("http://ec.urbentom.co.uk/studentAppData.xml"));
                }            
            }

            private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                setupDictionary(); 
                MainPage page = new MainPage();
                if (e.Error != null)
                    return;
                XElement xmlitems = XElement.Parse(e.Result);
                List<XElement> elements = xmlitems.Descendants("item").ToList();

                List<StudentGuideModel> moveItems = new List<StudentGuideModel>();
                foreach (XElement c in elements)
                {
                    StudentGuideModel _item = new StudentGuideModel();

                    _item.Title = c.Element("Title").Value;
                    _item.Description = c.Element("Description").Value;
                    _item.Phone = c.Element("Phone").Value;
                    _item.Email = c.Element("Email").Value;
                    _item.Category = c.Element("Category").Value;
                    _item.Image = c.Element("Image").Value;
                    _item.SmallInfo = c.Element("SmallInfo").Value;
                    _item.Image = getImage(_item.Image);
                    allData.Add(_item);
                }
                MessageBox.Show("Download Complete", "Loaded", MessageBoxButton.OK);                                                          
            }

填充collectionB&amp;的方法collectionC

public ObservableCollection<StudentGuideModel> splitCategories(string catName)
{
    ObservableCollection<StudentGuideModel> catItems = new ObservableCollection<StudentGuideModel>();
    foreach (var item in allData)
    {
        if (item.Category == catName)
        {
            catItems.Add(item);
        }
    }
    return catItems;
}

使用collectionB&amp;的列表人口collectionC

faciliiesList.ItemsSource = App.getControl.splitCategories("Facilities");
contactPanel.ItemsSource = App.getControl.splitCategories("Contacts");  

1 个答案:

答案 0 :(得分:0)

我将假设您首先调用downloadXML,然后在page_Loaded事件处理程序中调用splitCategories。 (顺便说一句,你应该遵循C#命名约定,你的方法应该是DownloadXML和SplitCategories)。

因为您的webclient订阅了DownloadStringCompleted事件,所以您的splitCategories可能会在下载字符串之前开始填充B和C.我建议你从NuGet获取HTTP Client库(Microsoft.Net.Http是NuGet上的id)。 HTTPClient使用async / await框架,它将为您完成工作。这是示例代码:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
string downloadedString = await client.GetStringAsync("http://ec.urbentom.co.uk/studentAppData.xml");
// Populate A here
// PopulateB here

注意,您必须将async关键字添加到Loaded事件处理程序。