2014-02-12 40 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); 
    }