2012-05-25 81 views
0

我從互聯網上拉取一個XML文件並將其加載到手機上。一切正常,減去應用程序初始加載時不更新的列表框。Windows Phone 7列表框沒有更新

該應用程序的簡單路徑如下:

在應用負載,它運行的更新聯繫人方法,如果其具有連接到互聯網。如果沒有,它會檢查它是否是第一次加載,如果是,它會在運行應用程序之前通知用戶連接到移動或無線網絡,否則它會加載舊數據。

在update方法中,它檢查web服務以查看自上次更新以來是否有任何更新。如果它的第一個負載,它說已有2000次更新,迫使更新。如果更新數大於零,它將運行下載並加載數據(這是此處的問題),否則它會加載舊數據(當前數據)。

問題出在下載之後,它不會加載數據。這是所述項目的代碼。

的功能的getData:

private void getData() 
     { 
      try 
      { 
       using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("contacts.xml", FileMode.Open); 
        using (StreamReader reader = new StreamReader(isoFileStream)) 
        { 
         XElement xmlContact = XElement.Parse(reader.ReadToEnd()); 
         lstContacts.ItemsSource = from contact in xmlContact.Descendants("contact") 
                   select new ContactItem 
                   { 
                    ImageSource = contact.Element("Image").Value, 
                    FName = contact.Element("FName").Value, 
                    LName = contact.Element("LName").Value, 
                    Extension = contact.Element("Extension").Value, 
                    Email = contact.Element("Email").Value, 
                    Cell = contact.Element("Cell").Value, 
                    Title = contact.Element("TitleName").Value, 
                    Dept = contact.Element("deptName").Value, 
                    Office = contact.Element("officename").Value, 
                    ID = contact.Element("ID").Value 
                   }; 
        } 
       } 
      } 
      catch 
      { 
      } 
     } 

更新功能:

void contact_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      if (e.Error != null) 
      { 
       return; 
      } 

      XElement xmlContact = XElement.Parse(e.Result); 
      string updates = xmlContact.Element("updates").Element("number").Value; 
      if (firstrun) 
      { 
       updates = "2000"; 
       settings["firstRun"] = (bool)false; 
      } 
      MessageBox.Show(updates); 
      if (Convert.ToInt32(updates) > 0) 
      { 
       MessageBox.Show("Updating Contacts"); 
       ContactReader cr = new ContactReader("http://domain.tld/RLContactApp/getContactsWP7.php?sqlQueryType=C&lastUpdated=01%2F01%2F2000&format=xml", "contacts.xml"); 
       bool downloaded = cr.Download(); 
       if (downloaded) 
       { 
        getData(); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Not Updating Contacts"); 
       getData(); 
      } 

     } 

我正在尋找一種方法,使數據得到加載到列表框中沒有告訴用戶關閉應用程序並重新加載它。

任何幫助,非常感謝。

回答

0

轉換通過在結束使用.toList()像

lstContacts.ItemsSource = (from contact in xmlContact.Descendants("contact") 
                   select new ContactItem 
                   { 
                    ImageSource = contact.Element("Image").Value, 
                    FName = contact.Element("FName").Value, 
                    LName = contact.Element("LName").Value, 
                    Extension = contact.Element("Extension").Value, 
                    Email = contact.Element("Email").Value, 
                    Cell = contact.Element("Cell").Value, 
                    Title = contact.Element("TitleName").Value, 
                    Dept = contact.Element("deptName").Value, 
                    Office = contact.Element("officename").Value, 
                    ID = contact.Element("ID").Value 
                   }).ToList<ContactItem>(); 

現在這將執行查詢並返回列表中,並且將能夠將數據綁定到你的結果LINQ查詢。

試一試 (query})。ToList();

+0

不是? ContactItem不包含.toList()方法,並且如果我將itemessource傳遞給IEnumerable .toList(),則它沒有任何區別。 –

+0

嘗試用這個 (查詢})。ToList (); – JSJ

+0

仍然不起作用=/ –