无法 Select 来自 Xmarin.Essentials.FilePicker 的文件。所有文件都被禁用

Unable to Select files from Xmarin.Essentials.FilePicker . All files are disable

当尝试获取文件时,所有文件都显示为禁用。我无法 select 这些文件。我正在使用以下代码。

var options = new PickOptions
                        {
                            PickerTitle = "Please select a pdf file",
                            FileTypes = FilePickerFileType.Pdf
                        };
                    var fileResult = await FilePicker.PickAsync(options);
                    var file = await Constants.ValidateFileNew(fileResult);
                    if (file.IsValid )
                    {
                        var fileName = file.FileName;
                        var data = file.fileData;
                        Stream stream = new MemoryStream(data);
                        fileID += 1;
                        ListNursingAssessmentFileNames.Add(new FileEntity { FileName = fileName, FileID = fileID, File = data });
                        DependencyService.Get<IJsonFileService>().SaveToLibrary<ObservableCollection<FileEntity>>(ListNursingAssessmentFileNames, "NursingInitialAssessmentFiles.json");
                        stackDigitalForm.IsVisible = false;
                    }

我已提供外部存储权限。 Storage Permission SS

您需要在创建 PickOptions 时指定自定义文件类型,它们可以根据平台进行自定义。

Per docs,PickOptions.FileTypes每个平台都有很多类型,

On Android and iOS the files not matching this list is only displayed grayed out. When the array is null or empty, all file types can be selected while picking. The contents of this array is platform specific; every platform has its own way to specify the file types. On Android you can specify one or more MIME types, e.g. "image/png"; also wild card characters can be used, e.g. "image/*". On iOS you can specify UTType constants, e.g. UTType.Image. On UWP, specify a list of extensions, like this: ".jpg", ".png".

您可以参考下面的示例代码:

我添加了一个触发选择事件的按钮,

 private void PickFileButton_Clicked(object sender, EventArgs e)
        {
            Task<FileResult> results = PickAndShow();
        }
      //for custom types
    async Task<FileResult> PickAndShow()
        {
            var customFileType =
   
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
                { { DevicePlatform.iOS, new[] { "com.adobe.pdf", "UTType.Image" } }, // or general UTType values
                { DevicePlatform.Android, new[] { "application/pdf", "image/*" } },
                { DevicePlatform.UWP, new[] { ".pdf", ".jpg", ".png" } },
                { DevicePlatform.Tizen, new[] { "*/*" , ".png" } },
                { DevicePlatform.macOS, new[] { "pdf" , "public.image" } },
        });
            try
            {
                var result = await FilePicker.PickAsync(new PickOptions
                {
                    PickerTitle = "Please select files",
                    FileTypes = customFileType,
                });
                if (result != null)
                {
                    FileName.Text = $"File Name: {result.FileName}";
                    if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                        result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
                    {
                        var stream = await result.OpenReadAsync();
                        FileImage.Source = ImageSource.FromStream(() => stream);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                // The user canceled or something went wrong
            }
            return null;
        }
    }

注意:不要忘记在清单文件中添加 WRITE_EXTERNAL_STORAGERAED_EXTERNAL_STORAGE

官方参考文档: https://docs.microsoft.com/en-us/xamarin/essentials/file-picker?context=xamarin%2Fxamarin-forms&tabs=android