我能夠訪問其類中的列表(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");
}
}
}
可以指導我回顧/事件處理程序的文檔? – Fabii 2012-07-31 20:46:46