2014-06-08 2 views
1

XML을 다운로드하고 데이터를 collectionA으로 정렬하는 방법이 있습니다. 그런 다음 collectionA을 2 개의 작은 콜렉션으로 분할하여 collectionB & collectionC 두 개의 목록을 채 웁니다. 이제 문제는 collectionA이 채워지고 분할되는 방법이 완료되기 전에 collectionA에 채워지는 시간이 있다는 것입니다.페이지가로드 된 후로드 메소드

어떻게 만들까요? collectionB & collectionCcollectionA이 채워질 때까지 대기합니까?

채우는 방법 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"); 
+0

b와 c의 tge population을 호출하는 코드를 추가하면 완료되기 전에 완료를 말한 이유를 알 수 있습니다. –

답변

0

page_Loaded 이벤트 핸들러에서 downloadXML을 먼저 호출 한 다음 splitCategories를 두 번째 호출한다고 가정합니다. (btw, C# 명명 규칙을 따라야하며, 메서드는 DownloadXML 및 SplitCategories 여야합니다.)

웹 클라이언트가 DownloadStringCompleted 이벤트를 구독하므로 splitCategories가 문자열을 다운로드하기 전에 B 및 C를 채우기 시작합니다. NuGet (Microsoft.Net.Http는 NuGet의 ID)에서 HTTP 클라이언트 라이브러리를 가져와야합니다. 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 이벤트 처리기에 추가해야합니다.

관련 문제