加载页面后的Load方法

本文关键字:Load 方法 加载 | 更新日期: 2023-09-27 18:07:25

我有一个方法,这是下载XML和排序它的数据到collectionA。然后我将collectionA分成两个较小的集合,collectionB &collectionC填充2个列表。现在我遇到的问题是,在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 &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 &collectionC

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

加载页面后的Load方法

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

因为你的web客户端订阅了DownloadStringCompleted事件,你的splitCategories可能在字符串被下载之前就开始填充B和C。我建议您从NuGet (Microsoft.Net)获取HTTP客户端库。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事件处理程序中。