2013-03-11 229 views
1

我已经建立了一个应用程序与其中的浏览器。它运行良好,但是当我尝试导航到像bla一样的地址时。 pdf webbrowser什么也没显示。 我解决了这个问题,自动打开Internet Explorer,如果地址链接到PDF文件。电话:网页浏览器不打开PDF文件(Windows Phone 8)

有没有更好的解决方案?我想在我自己的应用程序中打开该PDF文件,我不想每次打开Internet Explorer。有什么建议么?

+0

我不认为这是建立在对当前操作系统的PDF文件的支持。 – 2013-03-11 14:13:07

+1

我发现了有关Windows应用商店应用的链接。我认为WP8也是如此:他们已经禁用了该功能作为安全防范措施。 http://blogs.msdn.com/b/wsdevsol/archive/2012/10/18/nine-things-you-need-to-know-about-webview.aspx#AN3 – Giorgio 2013-03-11 14:14:39

+0

你可以分享代码如何自动当pdf或文档存储在独立存储中时打开IE?我也试图做同样的事情,它不工作。 – Debhere 2013-03-27 06:53:51

回答

1

如果您有本地下载的独立存储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, see section on "launching a file")。

如果是远程URL,那么您可以使用LaunchUriAsync(它将使用IE首先下载文件)。

您需要在已安装PDF阅读器应用程序的设备上试用此功能 - 它不适用于模拟器。

+0

这似乎只工作一次。如果我点击我列表中的另一个项目,则浏览器卡在“点击打开”。网址在线,每次点击列表项时都会有所不同,这消除了混乱 – JDeVil 2013-11-28 05:31:50

0

你应该阅读下面的文章,如果您不熟悉异步:与异步MSDN异步编程并等待

,因为我的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 
    } 
} 

要呼叫您的web浏览器,控制这些方法,你需要赶上导航事件。

YourWebBrowserControl.Navigating += YourWebBrowserControl_Navigating; 

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

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

0

试试这个从WebControl的打开PDF:

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