Home > Net >  UWP open pdf fails when publish
UWP open pdf fails when publish

Time:11-15

i have this methods working fine when i'm on debug and on my pc:

public void ShowPdf(byte[] pdfInfo)
{
    ...
    Device.BeginInvokeOnMainThread(async () =>
    {
        var intentHelper = DependencyService.Get<IIntentHelper>();
        intentHelper.File(pdfInfo);
    });
}

And the dependency service like that:

[assembly: Xamarin.Forms.Dependency(typeof(IntentHelperUWP))]
namespace myApp.UWP
{
    class IntentHelperUWP : IIntentHelper
    {
        public async Task FileAsync2(byte[] array)
        {
            var baseUrl = DependencyService.Get<IBaseUrl>().Get();
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder; 
            StorageFile pdfFile = await storageFolder.CreateFileAsync("test.pdf", CreationCollisionOption.ReplaceExisting);
            //write data to created file
            await FileIO.WriteBytesAsync(pdfFile, array);
            //get asets folder
            StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder assetsFolder = await appInstalledFolder.GetFolderAsync("Assets");
            //move file from local folder to assets
            await pdfFile.MoveAsync(assetsFolder, "test.pdf", NameCollisionOption.ReplaceExisting);

         Device.BeginInvokeOnMainThread(async () =>
         {
             Windows.System.LauncherOptions options = new Windows.System.LauncherOptions();
             options.DisplayApplicationPicker = true;
             options.ContentType = "application/pdf";

             Windows.System.Launcher.LaunchFileAsync(pdfFile);
         });  
    }

Why it's working fine in debug with visual studio but not when i publish? i tried to publish release and debug, look if the pdf is set to content and copy all in properties, but every time i publish and test, the button to download pdf does nothing, but in my debug open the Adode reader with the PDF. Some hints of what i can do or test?

CodePudding user response:

Once packaged the install folder is read only. You cannot copy the file into the assets folder. This is the same in UWP, iOS, and Android.

You’ll need to launch it from the app data location you create it in rather than trying to move it into the installed package.

It works when debugging without publishing because the unpackaged app has full access to its working directory.

  • Related