gpt4 book ai didi

uno-platform - GetFileFromApplicationUriAsync、CopyAsync、AsStreamForRead 未在 Uno 平台中实现。解决方法?

转载 作者:行者123 更新时间:2023-12-01 15:02:10 25 4
gpt4 key购买 nike

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

是否有任何 Xamarin.Essentials 替代品

或其他NuGet 包

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

是否有可能直接在 Uno 中实现

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

我使用的是 Uno.UI v1.45.0。

最佳答案

作为大卫·奥利弗 pointed out in his answer ,

Uno hasn't implemented most of the Windows.StorageFile APIs, as for the most part there are alternatives available in System.IO, which will work cross-platform.

所以...

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

关于uno-platform - GetFileFromApplicationUriAsync、CopyAsync、AsStreamForRead 未在 Uno 平台中实现。解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58131778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com