2013-03-11 120 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); 
    } 
}