Launching PDF reader on windows phone 8

偶尔善良 提交于 2020-01-11 11:18:02

问题


I'm trying to launch pdf reader with the code below but it does not work. Can somebody help me?

    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.");
         }
    }

result is always false and so launcher is not presented.

I used LaunchUriAsync because LaunchFileAsync is not implemented on Windows Phone.


回答1:


LaunchUriAsync isn't supported on Windows Phone 8 per the documentation. It throws an exception if called

You can use Windows.System.Launcher.LaunchFileAsync to launch a StorageFile.

This code works for example (assming there's a file called "metro.pdf" in the project, with the Build Action set to Content, with Copy to Output Directory set to 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);



回答2:


Called the API and saved the byte array to file

    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);
                }
            }
        }
    }

opened the file in pdf reader using

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

localFolder is Windows.Storage.ApplicationData.Current.LocalFolder




回答3:


just put the anyFile.pdf in Assets folder, and make its build action to Content, and then Just make the function Async ... and then Put "await" before Windows.System.Launcher.LaunchFileAsync(pdf); it worked fine for me. Nice. See this.

     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);
    }


来源:https://stackoverflow.com/questions/21741339/launching-pdf-reader-on-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!