1

我正在尝试使用下面的代码启动 pdf 阅读器,但它不起作用。有人可以帮助我吗?

    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

3 回答 3

4

LaunchUriAsync根据文档,Windows Phone 8 不支持。如果调用它会抛出异常

您可以使用Windows.System.Launcher.LaunchFileAsync启动一个StorageFile.

"metro.pdf"例如,此代码有效(假设在项目中调用了一个文件,Build Action设置为ContentCopy 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);
于 2014-02-13T01:23:27.503 回答
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);
                }
            }
        }
    }

使用 pdf 阅读器打开文件

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

localFolder 是 Windows.Storage.ApplicationData.Current.LocalFolder

于 2014-10-02T17:21:30.367 回答
0

只需将 anyFile.pdf 放在 Assets 文件夹中,并对 Content 进行构建操作,然后将函数设为 Async ... 然后将“await”放在 Windows.System.Launcher.LaunchFileAsync(pdf); 它对我来说很好。好的。看到这个。

     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);
    }
于 2015-05-22T10:58:30.783 回答