对于课程,我们必须遵循教程来创建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;
}在给buttonSearch加套时,我们得到以下错误:
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文件,我们可以使用它,但它仍然不起作用)
我不知道是什么原因造成的,我们也没有从老师那里得到太多帮助,所以我希望有人能帮助我们。
谢谢,托马斯
发布于 2011-10-19 20:05:28
对于这段代码:
if (e.Error != null)
{
DisplayStories(e.Result);
}您的意思是,如果e.Error不为空,则显示故事。我认为您应该将条件转换为e.Error == null,因为这将意味着没有错误,并且使用结果是安全的。您可能希望在条件处放置一个断点,以检查e.Error的值,看看那里是否有异常。
编辑:
当您将条件更改为e.Error == null但没有发生任何事情时,这是因为错误不是空的,所以您的DisplayStories(e.Result)语句永远不会触发。
出现问题的例外是SecurityException,因为Silverlight浏览器内应用程序不允许您调用外部网站,除非该网站具有Silverlight跨域策略文件。不幸的是,Digg的策略文件不再允许跨域访问,这意味着除非你在浏览器外以完全信任的方式运行你的应用程序,否则你将无法进行这种调用。有关更多详细信息,请参阅Network Security Access Restriction in Silverlight。
若要以完全信任的方式将应用程序作为浏览器外应用程序运行,请在visual studio中右击项目并选择“属性”。在"Silverlight“选项卡上,选中”启用浏览器耗尽“复选框。然后单击显示“超出浏览器设置”的按钮。在对话框中,选中“在浏览器外运行时需要提升信任”的复选框。在"Debug“选项卡中,对于"Start Action”,选择"Out of browser application“,然后从下拉菜单中选择您的项目。
当您以这种方式运行时,您应该不再获得SecurityException。
https://stackoverflow.com/questions/7817619
复制相似问题