在Silverlight中加载MainPage_Loaded之前运行WebClient.DownloadStringCompleted

时间:2011-09-26 14:13:26

标签: silverlight webclient

我想在MainPage_Loaded

之前加载此事件处理程序
WebClient wc = new WebClient(); 

wc.DownloadStringCompleted += new 
DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);

wc.DownloadStringAsync(new Uri("./ImageList.xml", UriKind.Relative));

void wc_DownloadStringCompleted(object sender, 
    System.Net.DownloadStringCompletedEventArgs e){.....}

1 个答案:

答案 0 :(得分:1)

我认为这是问题 我想在加载MainPage之前“加载一些资源”“异步”!

WebClient wc = new WebClient(); 

wc.DownloadStringCompleted += 
            new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);

wc.DownloadStringAsync(new Uri("./ImageList.xml", UriKind.Relative));

void wc_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e){.....}

答案

如果它是您的应用程序的主页面,那么您可以将此代码添加到Application_Startup事件处理程序中的“app.xaml.cs”。请注意,由于您是异步加载资源,因此您应该在事件处理程序中加载主页而不是Application_Startup,除非您在完成资源加载过程之前不关心主页是否已加载。