C# 电话:webbrowser不打开PDF文件(Windows phone 8)

C# 电话:webbrowser不打开PDF文件(Windows phone 8),c#,windows-phone-8,C#,Windows Phone 8,我已经建立了一个应用程序,其中有一个网络浏览器。它工作得很好,但是当我尝试导航到像bla这样的地址时。pdf网络浏览器什么也不显示。 我解决了这个问题,如果地址链接到pdf文件,自动打开Internet Explorer 有更好的解决办法吗?我想在我自己的应用程序中打开PDF文件,但我不想每次都打开Internet Explorer。有什么建议吗?如果您在本地下载的PDF位于独立存储中,您可以使用LaunchFileAsync启动PDF阅读器应用程序(或注册为打开PDF文件的任何其他应用程序)

我已经建立了一个应用程序,其中有一个网络浏览器。它工作得很好,但是当我尝试导航到像bla这样的地址时。pdf网络浏览器什么也不显示。 我解决了这个问题,如果地址链接到pdf文件,自动打开Internet Explorer


有更好的解决办法吗?我想在我自己的应用程序中打开PDF文件,但我不想每次都打开Internet Explorer。有什么建议吗?

如果您在本地下载的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);
}
(改编自)

如果是远程URL,则可以使用
LaunchUriAsync
(首先使用IE下载文件)


您需要在安装了PDF Reader应用程序的设备上尝试此操作-它在模拟器上不起作用。

如果您在本地下载的PDF位于独立存储中,则可以使用
启动文件异步
启动PDF Reader应用程序(或注册以打开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);
}
(改编自)

如果是远程URL,则可以使用
LaunchUriAsync
(首先使用IE下载文件)


您需要在安装了PDF Reader应用程序的设备上尝试此操作-它在模拟器上不起作用。

如果您不熟悉Async,则应阅读以下文章:MSDN使用Async异步编程并等待

我无法测试我的应用程序,因为我的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控件调用这些方法,需要捕获导航事件

YourWebBrowserControl.Navigating += YourWebBrowserControl_Navigating;

void YourWebBrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
    if(e.Uri.AbsolutPath.EndsWith("pdf"))
    {
        StartPDFDownload(e.Uri.ToString());
    }
}

不要忘记,有一天您将不得不删除创建的文件。

如果您不熟悉Async,您应该阅读以下文章:MSDN使用Async异步编程并等待

我无法测试我的应用程序,因为我的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控件调用这些方法,需要捕获导航事件

YourWebBrowserControl.Navigating += YourWebBrowserControl_Navigating;

void YourWebBrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
    if(e.Uri.AbsolutPath.EndsWith("pdf"))
    {
        StartPDFDownload(e.Uri.ToString());
    }
}

别忘了,您将不得不删除某一天创建的文件。

尝试以下方法从网络控件打开PDF:

void MyWebBrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
    if (e.Uri.AbsolutPath.ToLower().EndsWith(".pdf"))
    {
        var success = Windows.System.Launcher.LaunchUriAsync(e.Uri);
    }
}

尝试从网络控件打开PDF:

void MyWebBrowserControl_Navigating(object sender, NavigatingEventArgs e)
{
    if (e.Uri.AbsolutPath.ToLower().EndsWith(".pdf"))
    {
        var success = Windows.System.Launcher.LaunchUriAsync(e.Uri);
    }
}

我认为目前操作系统中没有内置的PDF文件支持。我找到了有关Windows应用商店应用程序的链接。我认为WP8也是这样:为了安全起见,他们已经禁用了该功能。当pdf或文档存储在isolatedstoreage中时,您可以分享如何自动打开IE的代码吗?我太想做同样的事情,但它不起作用。我不使用隔离存储。它只是一个启动手机网站的应用程序。我只是检查下一个单击的链接是否是pdf文档。我认为当前操作系统中没有内置的对pdf文件的支持。我找到了有关Windows应用商店应用程序的此链接。我认为WP8也是这样:为了安全起见,他们已经禁用了该功能。当pdf或文档存储在isolatedstoreage中时,您可以分享如何自动打开IE的代码吗?我太想做同样的事情,但它不起作用。我不使用隔离存储。它只是一个启动手机网站的应用程序。我只是检查下一个点击的链接是否是pdf文档。这似乎只起作用一次。如果我单击列表中的另一项,则浏览器会卡在“点击打开”上。url是在线的,每次单击列表项时都不一样,这消除了Chachingt这似乎只起作用一次。如果我单击列表中的另一项,则浏览器会卡在“点击打开”上。url是在线的,每次单击列表项时都不一样,这消除了不一致