对于课程,我们必须按照教程创建一个 Silverlight 网站,该网站在 DIGG 中搜索给定主题。(以本教程为基础:http ://weblogs.asp.net/scottgu/archive/2010/02/22/first-look-at-silverlight-2.aspx )
我们必须使用以下代码从 DIGG 获取信息。
private void buttonSearch_Click(object sender, RoutedEventArgs e)
{
string topic = textboxSearchTopic.Text;
WebClient digg = new WebClient();
digg.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(digg_DownloadStringCompleted);
digg.DownloadStringAsync(
new Uri("http://services.digg.com/1.0/story.getAll?count=10&topic="+topic));
}
void digg_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
DisplayStories(e.Result);
}
}
private void DisplayStories(string xmlContent)
{
XDocument document = XDocument.Parse(xmlContent);
var stories = from story in document.Descendants("story")
where story.Element("thumbnail")!=null
select new DiggStory
{
Id = (string)story.Attribute("id"),
Title = (string)story.Element("title"),
Description = (string)story.Element("description"),
ThumbNail = (string)story.Element("thumbnail").Attribute("src"),
HrefLink = (string)story.Attribute("link"),
NumDiggs = (int)story.Attribute("diggs")
};
gridStories.ItemsSource = stories;
}
当插入按钮搜索时,我们得到错误:
An exception occurred during the operation, making the result invalid. Check InnerException for exception details.
at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
at System.Net.OpenReadCompletedEventArgs.get_Result()
at DiggSample.Views.Search.Digg_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
at System.Net.WebClient.OpenReadOperationCompleted(Object arg)
我已经知道 Digg API 已经过时了,但我认为这个错误与它没有任何关系。(我们甚至得到了一个本地 XML 文件,我们可以使用它但它仍然不起作用)
我不知道是什么原因造成的,我们也没有从老师那里得到太多帮助,所以我希望有人能帮助我们。
谢谢,托马斯