【发布时间】:2011-10-08 12:16:40
【问题描述】:
我正在尝试使用 silverlight 获取页面的 html 内容。 Webresponse 和 request 类在 silverlight 中不起作用。
我做了一些谷歌搜索,我发现了一些东西。这是我尝试过的:
public partial class MainPage : UserControl
{
string result;
WebClient client;
public MainPage()
{
InitializeComponent();
this.result = string.Empty;
this.client = new WebClient();
this.client.DownloadStringCompleted += ClientDownloadStringCompleted;
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
string url = "http://www.nu.nl/feeds/rss/algemeen.rss";
this.client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
if (this.result != string.Empty && this.result != null)
{
this.txbSummery.Text = this.result;
}
}
private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
this.result = e.Result;
//handle the response.
}
}
按下按钮后出现运行时错误:
Microsoft JScript 运行时错误:Silverlight 应用程序中的未处理错误 操作过程中发生异常,导致结果无效。检查 InnerException 以获取异常详细信息。在 System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() 在 System.Net.DownloadStringCompletedEventArgs.get_Result() 在 JWTG.MainPage.ClientDownloadStringCompleted(对象发送者,DownloadStringCompletedEventArgs e) 在 System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) 在 System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
我尝试了很多事情,但都失败了。
我错过了什么?或者有谁知道我如何以不同的方式实现这一目标?
提前致谢!
【问题讨论】:
-
您遇到了 JScript 错误。您发布的代码与错误无关。我刚试过你的sn-p,它可以工作。顺便说一句:您必须将
this.txbSummery.Text = this.result;移动到您的ClientDownloadStringCompleted方法中。目前,您正试图在下载文本之前将其放入文本框中。这显然行不通。 -
看看:forums.silverlight.net/forums/t/54721.aspx 也许对你有帮助。问题可能是配置错误的
web.config文件。
标签: c# html silverlight runtime