2012-07-31 312 views
0

我能夠訪問其類中的列表(ListData)value,但是如果我嘗試從類的外部引用列表,它會將其顯示爲空列表。爲什麼列表爲空?

我在getVideoList方法中將對象添加到ListData中。

我添加了這段代碼,它現在可以工作(輪詢方法),這是不是很方便?

IAsyncResult result = request.BeginGetResponse(new AsyncCallback(getVideoList), state); 
      while (result.IsCompleted == false) 
      { 
       Thread.Sleep(1000); 
      } 

Public class otherclass{ 
    static void main(string [] args){ 
     RestProcess process = new RestProcess(videoListURI); 
     process.initilaizeRest(); 

     foreach (VideoSeriesElement ele in process.ListData) 
     { 
     Console.WriteLine(ele.id); 
     Console.WriteLine(ele.title); 
     Console.WriteLine(ele.href); 
     Console.WriteLine(ele.image); 
     } 

     Console.ReadLine(); 
    } 

} 

public class RestProcess 
{ 
    /// <summary> 
    /// Default constructor 
    /// </summary> 
    /// 
    readonly string blankimage = "http://images1.test.com/properties/NoThumbnail_HD.jpg"; 
    public RestProcess(string uri) 
    { 
     URI = uri; 
     ListData = new ObservableCollection<VideoSeriesElement>(); 
    } 
    public ObservableCollection<VideoSeriesElement> ListData{get;set;} 


    public void initilaizeRest() 
    { 
     WebRequest request = HttpWebRequest.Create(URI); 
     request.Method = "GET"; 

     // RequestState is a custom class to pass info to the callback 
     RequestState state = new RequestState(request, URI); 
     IAsyncResult result = request.BeginGetResponse(new AsyncCallback(getVideoList), state); 
    } 

    public string URI{get;set;} 

    /// <summary> 
    /// getVideoList 
    /// </summary> 
    /// <param name="result"></param> 
    public void getVideoList(IAsyncResult result) 
    { 
     RequestState state = (RequestState)result.AsyncState; 
     WebRequest request = (WebRequest)state.Request; 
     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); 

     try 
     { 
      TextReader textreader = new StreamReader(response.GetResponseStream()); 
      XDocument doc = XDocument.Load(textreader); 


      foreach(XElement videoSeriesEle in doc.Descendants("video-series")) 
      { 
       var videseriesEle = new VideoSeriesElement();  
       videseriesEle.id = videoSeriesEle.Attribute("id").Value; 
       videseriesEle.href = videoSeriesEle.Attribute("href").Value; 
       videseriesEle.title = videoSeriesEle.Element("title").Value; 

       if (!videoSeriesEle.Element("images").IsEmpty && videoSeriesEle.Element("images").Elements("image") != null) 
       { 
        var imagelist = videoSeriesEle.Element("images").Elements("image").ToList(); 
        foreach (var ele in imagelist) 
        { 
         if (ele.Attribute("styleid").Value.Equals("5")) 
         { 
          if (videoSeriesEle.Element("images").Element("image").Attribute("href").Value != null) 
          { 
           videseriesEle.image = videoSeriesEle.Element("images").Element("image").Attribute("href").Value; 
          } 

         } 
        } 

       } 
       else { videseriesEle.image = blankimage; } 


       //add object to list 
       ListData.Add(videseriesEle); 

      } 

      //Close streams 
      textreader.Close(); 
      response.Close(); 
     } 
     catch (XmlException e) 
     { 
      Console.WriteLine(e); 
     } 

    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="state"></param> 
    /// <param name="timedOut"></param> 
    private static void ScanTimeoutCallback(object state, bool timedOut) 
    { 
     if (timedOut) 
     { 
      RequestState reqState = (RequestState)state; 
      if (reqState != null) 
       reqState.Request.Abort(); 
      Console.WriteLine("aborted- timeout"); 
     } 
    } 
} 

回答

2

RestProcess.initilaizeRest()該方法經由IAsyncResult請求數據,這意味着調用來獲取數據是異步的。在您的otherClass課程中,您嘗試在呼叫initilaizeRest()後立即處理並循環訪問ListData元素,並且很有可能數據尚未加載。

要解決這個問題,您應該在otherClass中有一個回調/事件處理程序,用於監聽數據何時填充。

+0

可以指導我回顧/事件處理程序的文檔? – Fabii 2012-07-31 20:46:46

1

您可以在RestProcess中的request.BeginGetResponse調用中暴露IAsyncResult結果,並在其他類中阻止該屬性,直到IAsyncResult.IsCompleted屬性爲true,或者使用它公開的WaitHandle,但有沒有原因使用異步BeginGetResponse調用而不是同步GetResponse調用?

+0

我不相信Xbox360支持GetResponse – Fabii 2012-07-31 20:21:21

+1

對不起,我沒有意識到您正在爲360開發。請查看這裏的幾個示例,瞭解如何使用實現像BeginGetRespoonse這樣的異步模式的東西:http:// www.dotnetcurry.com/ShowArticle.aspx?ID=642 - 投票和回調。 – Cashley 2012-07-31 20:58:33

+0

非常感謝。 – Fabii 2012-07-31 21:26:15

相關問題