【问题标题】:UWP open pdf fails when publish发布时 UWP 打开 pdf 失败
【发布时间】:2022-11-14 14:26:52
【问题描述】:

当我在调试和我的电脑上时,我有这个方法工作正常:

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

和这样的依赖服务:

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

为什么它在使用 Visual Studio 进行调试时工作正常,但在我发布时却不行?我尝试发布和调试,查看 pdf 是否设置为内容并复制所有属性,但每次我发布和测试时,下载 pdf 的按钮什么都不做,但在我的调试中打开带有 PDF 的 Adode 阅读器。我可以做什么或测试的一些提示?

【问题讨论】:

  • 添加一些异常处理和日志记录以确定发生了什么
  • 我正在使用 var messageDialog = new MessageDialog(e.Message);等待 messageDialog.ShowAsync();在调试中工作正常,但在已发布的版本上,当输入“Device.BeginInvokeOnMainThread(async () =>...”时它会崩溃
  • 将其记录到控制台,或使用像 appcenter.ms 这样的崩溃报告工具

标签: xamarin uwp


【解决方案1】:

打包后,安装文件夹是只读的。您不能将文件复制到资产文件夹中。这在 UWP、iOS 和 Android 中都是一样的。

您需要从创建它的应用程序数据位置启动它,而不是尝试将其移动到已安装的包中。

它可以在不发布的情况下进行调试,因为未打包的应用程序对其工作目录具有完全访问权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 2016-04-30
    • 2016-04-30
    • 2018-06-19
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多