2012-07-15 28 views

回答

0

我會用一個單一的WebClient例如下載的文件,並簡單地將其降低到一個單一的事件處理程序。更重要的是,你可以看到類似的文檔結構。這意味着你也可以重複使用你的閱讀代碼,而不是兩次寫。

事情是這樣的:

void Download(GameType type, Action onCompletion) 
{ 
    WebClient client = new WebClient(); 
    client.DownloadProgressChanged += (s, args) => 
    { 
     // Handle change in progress. 
    }; 

    client.DownloadStringCompleted += (s, args) => 
    { 
     XDocument feed = XDocument.Parse(args.Result); 

     var itemSource = from query in feed.Root 
             select new NewGamesClass 
             { 
              NewGameTitle = (string)query.Element("Title"), 
              NewGameDescription = (string)query.Element("Descript 
              NewGameImage = (string)query.Element("Image") 
             }; 
     switch (type) 
     { 
      case GameType.ComingSoon: 
       { 
        ComingSoonList.ItemSource = itemSource; 
       } 
      case GameType.NewGames: 
       { 
        NewGameList.ItemSource = itemSource; 
       } 

     } 

     if (onCompletion != null) 
      onCompletion(); 
    }; 

    switch (type) 
    { 
     case GameType.NewGames: 
      { 
       client.DownloadStringAsync(new Uri("http://microsoft.com", UriKind.Absolute)); 
       break; 
      } 
     case GameType.ComingSoon: 
      { 
       client.DownloadStringAsync(new Uri("http://www.bing.com", UriKind.Absolute)); 
       break; 
      } 
    } 
} 

雖然代碼看起來複雜一點,它讓當特定數據集下載您遞歸地下載數據。顯然,你將不得不聲明GameType枚舉,我只是用了一堆測試值來證明這個想法。

+0

但事情是他在列表框中有值,他想要獲取每個水龍頭或選擇列表項的詳細信息。使用你的方法,它會在每次點擊時發送服務器調用,這不是很好的方法。 – 2012-07-16 05:49:22

+0

取決於他需要操作的數據。從我所瞭解的情況來看,他希望每次用戶點擊時更新它,所以這種方法更加可以接受。 – 2012-07-16 06:31:47

+0

嗨,我已經安裝了你的應用程序。並導航到該頁面,但沒有鏈接。你可以請分享一些代碼或一些示例應用程序。所以我可以更好地幫助你。謝謝 – 2012-07-17 05:54:28

0
// It Will Download The XML Once To Use further to Avoid Again And Again Calls For Each Time 


public void GetXML(string path) 
      { 
       WebClient wcXML = new WebClient(); 
       wcXML.OpenReadAsync(new Uri(path)); 
       wcXML.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient); 
      } 

void webClient(object sender, OpenReadCompletedEventArgs e) 
     { 
      if (e.Error == null) 
      { 
       try 
       { 
        Stream Resultstream = e.Result; 
        XmlReader reader = XmlReader.Create(Resultstream); 

         var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication(); 
         using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Download.xml", System.IO.FileMode.Create, isolatedfile)) 
         { 
          byte[] buffer = new byte[e.Result.Length]; 
          while (e.Result.Read(buffer, 0, buffer.Length) > 0) 
          { 
           stream.Write(buffer, 0, buffer.Length); 
          } 
          stream.Flush(); 
          System.Threading.Thread.Sleep(0); 
         } 
       } 

       catch (Exception ex) 
       { 
        //Log Exception 
       } 
      } 

      if (e.Error != null) 
      { 
       //Log Exception 
      } 
     } 

// This Method Will Give You Required Info According To Your Tag Like "Achievement123" 

protected List<DownloadInfo> GetDetailFromXML(string TagName) 
     { 
      //TagName Like "achivement23" 
      List<DownloadInfo> listDetails = new List<DownloadInfo>(); 

      XDocument loadedData; 
      try 
      { 
       using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        // Check For Islated Storage And Load From That 
        if (storage.FileExists("Download.xml")) 
        { 
         using (Stream stream = storage.OpenFile("Download.xml", FileMode.Open, FileAccess.Read)) 
         { 
          loadedData = XDocument.Load(stream); 
          //TagName Like "achivement23" 
          listDetails.AddRange((from query in loadedData.Element("GOW1").Elements(TagName) 
                select new DownloadInfo 
                { 
                 TITLE = (string)query.Element("TITLE"), 
                 DESCRIPTION = (string)query.Element("DESCRIPTION"), 
                 ACHIVEMENTIMAGE = (string)query.Element("ACHIVEMENTIMAGE"), 
                 GUIDE = (string)query.Element("GUIDE"), 
                 YOUTUBELINK = (string)query.Element("YOUTUBELINK") 
                }).ToList()); 
         } 
        } 
       } 
       return listDetails; 
      } 
      catch (Exception ex) 
      { 
       return listDetails = null; 
       //Log Exception 
      } 
     } 

     public class DownloadInfo 
     { 
      public string TITLE { get; set; } 
      public string DESCRIPTION { get; set; } 
      public string GUIDE { get; set; } 
      public string ACHIVEMENTIMAGE { get; set; } 
      public string YOUTUBELINK { get; set; } 
     } 

這裏是你的方法調用

GetXML("ANY URL FROM WHERE YOU WANT TO GET XML"); // You Will Download All The XML Once To Avoid Again And Again Server Calls To Get XML. 

GetDetailFromXML("YOUR TAG NAME"); // It Will Take Your Tag Name Which Information You Want To Get Like "Acheicement123" and It Will Return You Data In List<DownloadInfo> and You can easily get data from this List. 

我希望這會幫助你。我沒有在運行時測試代碼。但我希望它會給你一些想法.. :)

相關問題