0

对于课程,我们必须按照教程创建一个 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 文件,我们可以使用它但它仍然不起作用)

我不知道是什么原因造成的,我们也没有从老师那里得到太多帮助,所以我希望有人能帮助我们。

谢谢,托马斯

4

1 回答 1

1

对于这部分代码:

if (e.Error != null)
{
    DisplayStories(e.Result);             
}

如果 e.Error不为空,您是说要显示这些故事。我认为您想将条件切换为 say e.Error == null,因为这意味着没有错误并且可以安全地使用结果。您可能希望在条件处放置一个断点以检查 的值e.Error以查看那里是否有异常。

编辑:

当您将条件更改为e.Error == null并且没有发生任何事情时,那是因为错误是非空的,因此您的DisplayStories(e.Result)语句从未触发。

有问题的例外是SecurityException,因为 Silverlight 浏览器内应用程序不允许您调用外部网站,除非该网站具有 Silverlight 跨域策略文件。不幸的是,Digg 的策略文件不再允许跨域访问,这意味着除非您在完全信任的浏览器外运行您的应用程序,否则您将无法进行此调用。有关详细信息,请参阅Silverlight中的网络安全访问限制。

To run your app as an out-of-browser app with full trust, in visual studio, right-click your project and choose properties. On the "Silverlight" tab, check that box that says "enable running out of browser." Then click the button that says "Out of browser settings." In the dialog, check the box that says "require elevated trust when running outside the browser." In the "Debug" tab, for "Start Action," choose "Out of browser application" and select your project from the dropdown.

When you run this way, you should no longer get the SecurityException.

于 2011-10-19T12:05:27.700 回答