您好我使用WebBrowserTask
下载pdf文件。下载pdf文件后,我想自动返回我的应用程序。我该怎么做呢?这是我的代码:
Microsoft.Phone.Tasks.WebBrowserTask wbt = new Microsoft.Phone.Tasks.WebBrowserTask();
wbt.Uri = new Uri(PDFPath);
wbt.Show();
答案 0 :(得分:0)
下载完成后,WebBrowserTask没有要触发的事件。所以,我不会用它来下载pdf,而是使用它:
WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri("http://www.somesite.com/media/PDF/xyz.pdf"));
然后完成的事件将在完成时触发:
async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
byte[] buffer = new byte[e.Result.Length];
await e.Result.ReadAsync(buffer, 0, buffer.Length);
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile.OpenFile("MyDownloadedFile.pdf", FileMode.Create))
{
await stream.WriteAsync(buffer, 0, buffer.Length);
}
}
App.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
Deployment.Current.Dispatcher.BeginInvoke(() =>
MessageBox.Show("The pdf has been downloaded!"
, "PDF Download Success", MessageBoxButton.OK)
);
}