2014-02-12 70 views
1

我試圖用下面的代碼啓動pdf閱讀器,但它不起作用。有人能幫助我嗎?在Windows Phone 8上啓動PDF閱讀器8

private async Task<StorageFile> WriteData(string fileName, byte[] data) 
    { 
     StorageFolder folder = ApplicationData.Current.LocalFolder; 

     StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); 

     using (Stream s = await file.OpenStreamForWriteAsync()) 
     { 
      await s.WriteAsync(data, 0, data.Length); 
      s.Close(); 
     } 

     return file; 
    } 

    private async Task<bool> OpenPdf(StorageFile file) 
    { 

     var uri = new Uri(file.Path, UriKind.RelativeOrAbsolute); 
     bool result = await Windows.System.Launcher.LaunchUriAsync(uri); 

     return result; 
    } 

    private async void FetchPdf() { 
     // Fetch pdf bytes to network 
     //.... 
     StorageFile file = await WriteData("test.pdf", data); 

     if (file != null) { 
      bool result = await OpenPdf(file);  
      if (result) 
       Debug.WriteLine("Success"); 
      else 
       Debug.WriteLine("Cannot open pdf file."); 
     } 
    } 

結果總是錯誤的,所以發射器不會顯示。

我使用了LaunchUriAsync,因爲LaunchFileAsync未在Windows Phone上實現。

回答

4

LaunchUriAsync在Windows Phone 8上不支持每documentation。如果調用它將引發異常

您可以使用Windows.System.Launcher.LaunchFileAsync啓動StorageFile

此代碼的工作,例如(assming有一個名爲"metro.pdf"在項目文件,與Build Action設置爲Content,與Copy to Output Directory設置爲Copy if Newer)。

var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation; 
var assets = await installedLocation.GetFolderAsync("Assets"); 
var pdf = await assets.GetFileAsync("metro.pdf"); 
Windows.System.Launcher.LaunchFileAsync(pdf); 
+0

由文檔在Visual Studio誤導,你可以在這裏看到:https://imagizer.imageshack.us/v2/881x175q90/401/pozq.png – rdon

+0

我可以測試這個模擬器上? – user2056563

+0

@ user2056563我不確定你在問什麼。 – WiredPrairie

0
調用

API和保存的字節數組到文件

public static async void WriteDataToIsolatedStorageFile(string fileName, byte[] data) 
    { 
     using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (IsolatedStorageFileStream stream = storageFile.OpenFile(fileName, FileMode.Create)) 
      { 
       if ((data != null) && (data.Length > 0)) 
       { 
        await stream.WriteAsync(data, 0, data.Length); 
       } 
      } 
     } 
    } 

打開使用

private async void StartExternalPDFApp() 
    { 
     StorageFolder localFolder = await FileManager.FindDirectory(FileManager.RelativeStorageDirectoryLocalStorage); 
     StorageFile storageFile = await localFolder.GetFileAsync(PdfFileName); 
     await Windows.System.Launcher.LaunchFileAsync(storageFile); 
    } 

localFolder在pdf閱讀器的文件是Windows.Storage.ApplicationData.Current.LocalFolder

0

只需將anyFile.pdf放在Assets文件夾中,並將其構建操作設置爲Content,然後將Just使函數Async ...然後在Windows.System.Launcher.LaunchFileAsync(pdf)之前放置「await」; 它爲我工作得很好。尼斯。 看到這個。

 private async void privacyPolicy_Click(object sender, EventArgs e) 
      { 
     var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation; 
     var assets = await installedLocation.GetFolderAsync("Assets"); 
     var pdf = await assets.GetFileAsync("PrivacyPolicy.pdf"); 
     await Windows.System.Launcher.LaunchFileAsync(pdf); 
    }