3

我尝试使用以下方法,但所有这些方法都显示为在 Uno (Android) 中实现。我能做些什么?

有没有 Xamarin.Essentials替代品

还是其他NuGet 包

或者我应该在每个平台上使用本机实现?

甚至可以直接在 Uno中实现它吗?

var pdfFile = StorageFile.GetFileFromApplicationUriAsync(..);
pdfFile.CopyAsync(..);
(await pdfFile.OpenReadAsync()).AsStreamForRead(); // AsStreamForRead() not implemented

我正在使用 Uno.UI v1.45.0。

4

2 回答 2

2

正如大卫奥利弗在他的回答中指出的那样

Uno 尚未实现大多数 Windows.StorageFile API,因为 System.IO 中的大多数情况下都有可用的替代方案,它们可以跨平台工作。

所以...

  1. 要从应用程序包中打开文件,我们可以将其构建操作设置为Embedded Resource而不是Content. StorageFile.GetFileFromApplicationUriAsync()我们可以使用以下代码代替方法:

    public Stream GetStreamFromResourceFile(string filename, Type callingType = null)
    {
        var assembly = (callingType ?? GetType()).Assembly;
        string foundResourceName = assembly.GetManifestResourceNames().FirstOrDefault(r => r.EndsWith(filename, StringComparison.InvariantCultureIgnoreCase));
        if (foundResourceName == null)
            throw new FileNotFoundException("File was not found in application resources. Ensure that the filename is correct and its build action is set to 'Embedded Resource'.", filename);
        return assembly.GetManifestResourceStream(foundResourceName);
    }
    
  2. 复制文件_

    await pdfFile.CopyAsync(..);
    

    我们改为:

    await pdfFile.CopyToAsync(newFile);
    
  3. 并获得一个阅读流

    (await pdfFile.OpenReadAsync()).AsStreamForRead();
    

    我们用:

    File.OpenRead(pdfFile);
    

所以最后我们有:

        string filename = "File.pdf";
        var pdfFile = GetStreamFromResourceFile(filename, GetType());
        string newFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
        using (var newFile = File.Create(newFilePath))
        {
            await pdfFile.CopyToAsync(newFile);
        }

        var fileStream = File.OpenRead(newFilePath);
于 2019-09-27T20:50:42.293 回答
0

Uno 还没有实现大多数Windows.StorageFileAPI,因为在大多数情况下,在 中都有可用的替代方案System.IO,它们可以跨平台工作。

但是,如果您尝试显示 pdf,则目前没有跨平台选项。在 Android 上显示 pdf 的最佳方式是启动一个意图,在 iOS 上可以将 pdf 显示为WebView.

Android的部分示例代码:

        public async Task Read(CancellationToken ct, string filePath)
        {
            var intent = new Intent(Intent.ActionView);

            var file = new Java.IO.File(filePath);
            var contentUri = Android.Support.V4.Content.FileProvider.GetUriForFile(ContextHelper.Current, _fileProviderAuthority, file);

            intent.SetFlags(ActivityFlags.GrantReadUriPermission);
            intent.SetDataAndType(contentUri, "application/pdf");

            StartActivity(intent);
        }

iOS的部分示例代码:

                    <ios:WebView 
                                 Source="{Binding FilePath}"
                                 HorizontalAlignment="Stretch"
                                 VerticalAlignment="Stretch" />
于 2019-09-27T15:20:58.920 回答