我已经构建了一个带有网络浏览器的应用程序。它工作正常,但是当我尝试导航到像 bla 这样的地址时。pdf网络浏览器什么也没显示。如果地址链接到 pdf 文件,我通过自动打开 Internet Explorer 解决了这个问题。
有更好的解决方案吗?我想在我自己的应用程序中打开那个 PDF 文件,我不想每次都打开 Internet Explorer。有什么建议么?
我已经构建了一个带有网络浏览器的应用程序。它工作正常,但是当我尝试导航到像 bla 这样的地址时。pdf网络浏览器什么也没显示。如果地址链接到 pdf 文件,我通过自动打开 Internet Explorer 解决了这个问题。
有更好的解决方案吗?我想在我自己的应用程序中打开那个 PDF 文件,我不想每次都打开 Internet Explorer。有什么建议么?
如果您在独立存储中有本地下载的 PDF,您可以使用LaunchFileAsync.
private async void LaunchFileButton_Click(object sender, RoutedEventArgs rea)
{
// Access isolated storage.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
// Access the PDF.
StorageFile pdfFile = await local.GetFileAsync("file1.pdf");
// Launch the bug query file.
Windows.System.Launcher.LaunchFileAsync(pdfFile);
}
(改编自MSDN,参见“启动文件”部分)。
如果它是一个远程 URL,那么您可以使用LaunchUriAsync(它将首先使用 IE 下载文件)。
您需要在安装了 PDF 阅读器应用程序的设备上尝试此操作 - 它无法在模拟器上运行。
如果您不熟悉 Async,您应该阅读以下文章:MSDN Asynchronous Programming with Async and Await
我无法测试我的应用程序,因为我的 WP8 手机当前不可用并且我无法在模拟器上安装 PDF 阅读器。
调用以下方法开始下载
WebClient pdfDownloader = null;
string LastFileName = ""; //To save the filename of the last created pdf
private void StartPDFDownload(string URL)
{
pdfDownloader = new WebClient(); //prevents that the OpenReadCompleted-Event is called multiple times
pdfDownloader.OpenReadCompleted += DownloadPDF; //Create an event handler
pdfDownloader.OpenReadAsync(new Uri(URL)); //Start to read the website
}
async void DownloadPDF(object sender, OpenReadCompletedEventArgs e)
{
byte[] buffer = new byte[e.Result.Length]; //Gets the byte length of the pdf file
await e.Result.ReadAsync(buffer, 0, buffer.Length); //Waits until the rad is completed (Async doesn't block the GUI Thread)
using (IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
LastFileName = "tempPDF" + DateTime.Now.Ticks + ".pdf";
using (IsolatedStorageFileStream ISFileStream = ISFile.CreateFile(LastFileName))
{
await ISFileStream.WriteAsync(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.HResult,
ex.Source, MessageBoxButton.OK);
//Catch errors regarding the creation of file
}
}
OpenPDFFile();
}
private async void OpenPDFFile()
{
StorageFolder ISFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
try
{
IStorageFile ISFile = await ISFolder.GetFileAsync(LastFileName);
await Windows.System.Launcher.LaunchFileAsync(ISFile);
//http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206987%28v=vs.105%29.aspx
}
catch (Exception ex)
{
//Catch unknown errors while getting the file
//or opening the app to display it
}
}
要从 WebBrowser-Control 调用这些方法,您需要捕获导航事件。
YourWebBrowserControl.Navigating += YourWebBrowserControl_Navigating;
void YourWebBrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
if(e.Uri.AbsolutPath.EndsWith("pdf"))
{
StartPDFDownload(e.Uri.ToString());
}
}
不要忘记您将不得不删除某天创建的文件。
试试这个从 WebControl 打开 PDF:
void MyWebBrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.AbsolutPath.ToLower().EndsWith(".pdf"))
{
var success = Windows.System.Launcher.LaunchUriAsync(e.Uri);
}
}