2011-10-19 38 views
0

對於課程,我們必須按照教程創建一個搜索DIGG以查找給定主題的silverlight網站。 (以本教程爲基礎:http://weblogs.asp.net/scottgu/archive/2010/02/22/first-look-at-silverlight-2.aspx使用Digg API和URI處理程序的未知錯誤(silverlight)

我們必須使用以下代碼從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文件,我們可以使用它,但它仍然不起作用)

我不知道是什麼原因造成這種情況,我們沒有從我們的老師那裏得到太多的幫助,所以我希望有人可以幫助我們。

感謝, 托馬斯

+0

什麼是InnerException? –

回答

1

對於這部分代碼:

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」選項卡上,選中「啓用瀏覽器耗盡」框。然後點擊標題爲「超出瀏覽器設置」的按鈕。在對話框中,選中「在瀏覽器外部運行時需要提升信任」的複選框。在「調試」選項卡中,對於「啓動操作」,選擇「瀏覽器外應用程序」,然後從下拉列表中選擇您的項目。

當你以這種方式運行時,你不應該再得到SecurityException。

+0

如果我將其更改爲==,它不會給我任何錯誤,也不會發生任何事情。我按下按鈕,但沒有任何反應。 稍後在教程中它告訴我們使用下面的代碼:http://pastebin.com/CYywS9Wx使用本地XML文件(digglifestyle.xml),使用該代碼我再次得到相同的錯誤。 感謝您的回覆! :) – Schoof

+0

這是e.Error的內容= http://pastebin.com/Khm7Tqbs – Schoof

+0

非常感謝你,這是工作!(Somewhat) 由於Digg API已過時並且無法工作,所以它不再提供結果,但是我找到了一個正常工作的XML頁面並且它工作正常!非常感謝 – Schoof

相關問題