phone:浏览器无法打开PDF文件(Windows phone 8)

本文关键字:phone 文件 Windows PDF 浏览器 | 更新日期: 2023-09-27 18:03:32

我已经建立了一个应用程序与网页浏览器在它。它工作得很好,但是当我试图导航到一个地址,比如bla。pdf浏览器没有显示任何内容。我解决了这个问题,如果地址链接到一个pdf文件,自动打开ie浏览器。

有更好的解决方案吗?我想在我自己的应用程序中打开那个PDF文件,我不想每次都打开ie浏览器。有什么建议吗?

phone:浏览器无法打开PDF文件(Windows phone 8)

如果你有一个本地下载的PDF文件在隔离存储中,你可以使用LaunchFileAsync启动PDF阅读器应用程序(或任何其他注册为打开PDF文件的应用程序)。

 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 Reader应用程序的设备上尝试-它在模拟器上不起作用

如果你不熟悉Async,你应该阅读以下文章:MSDN异步编程与Async和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);
    }
}