在WebBrowserTask之后如何自动转到应用程序wp8 C#

时间:2014-03-01 07:40:50

标签: c# windows-phone-8

您好我使用WebBrowserTask下载pdf文件。下载pdf文件后,我想自动返回我的应用程序。我该怎么做呢?这是我的代码:

Microsoft.Phone.Tasks.WebBrowserTask wbt = new Microsoft.Phone.Tasks.WebBrowserTask();

wbt.Uri = new Uri(PDFPath);
wbt.Show();

1 个答案:

答案 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)
            );
}

另外,我需要对Tutan的回答here给予赞扬,我只是稍微调整了一下。