我不确定为什么回调方法不会被触发。我正在使用VS 2010.
static void Main(string[] args)
{
try
{
var url = "some link to RSS FEED";
var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadStringAsync(new Uri(url));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// THIS IS NEVER FIRED
static void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Console.WriteLine("something");
}
// THIS IS NEVER FIRED
static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Console.WriteLine("do something");
var rss = XElement.Parse(e.Result);
var pictures = from item in rss.Descendants("channel")
select new Picture
{
Name = item.Element("title").Value
};
foreach (var picture in pictures)
{
Console.WriteLine(picture.Name);
Console.WriteLine(picture.Url);
}
}
答案 0 :(得分:3)
如果您调用DownloadDataCompleted
方法,则会触发DownloadDataAsync()
事件。如果您调用DownloadStringCompleted
方法,则会触发DownloadStringAsync()
。
要启动DownloadDataCompleted事件,请尝试:
static void Main(string[] args)
{
try
{
var url = "http://blog.gravitypad.com";
//client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadDataAsync(new Uri(url));
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
答案 1 :(得分:1)
我遇到了这个问题,并意识到uri不正确。我的意思是除非正确读取文件,否则事件不会触发。所以我将我的xml文件放在ClientBin中,它就像魔术一样!