You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI应用在MacCatalyst下无法选择文件问题求助

.NET MAUI MacCatalyst 文件选择器文件灰色无法选中的解决办法

问题核心原因

MacCatalyst 遵循 macOS 的统一类型标识符(UTI)规则,不支持 */* 这种通配符格式的文件类型配置,导致文件选择器无法匹配任何文件,最终所有文件呈灰色不可选中状态。此外,权限描述缺失也可能加剧访问限制。

具体解决步骤

  1. 修正 MacCatalyst 的文件类型配置
    替换原代码中 MacCatalyst 对应的 */*,改用苹果标准的 UTI 标识符,比如 public.data(匹配所有数据文件)或 public.item(匹配所有文件类型)。示例如下:

    private async void PickFileButton_Clicked(object sender, EventArgs e)
    {
        try
        {
            var result = await FilePicker.PickAsync(new PickOptions
            {
                FileTypes = new FilePickerFileType(
                    new Dictionary<DevicePlatform, IEnumerable<string>>
                    {
                        { DevicePlatform.iOS, new[] { "public.item" } },
                        { DevicePlatform.Android, new[] { "*/*" } },
                        { DevicePlatform.WinUI, new[] { "*/*" } },
                        { DevicePlatform.MacCatalyst, new[] { "public.data", "public.item" } }
                    })
            });
    
            if (result != null)
            {
                filePath = result.FullPath;
                await FilePathLabel.Dispatcher.DispatchAsync(() =>
                {
                    FilePathLabel.Text = filePath;
                });
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"File picking failed: {ex.Message}");
        }
    }
    
  2. 完善 Info.plist 的权限描述
    除了已设置的 LSSupportsOpeningDocumentsInPlace 和 UIFileSharingEnabled,还需添加文件夹访问权限的描述,否则系统会限制应用对文件目录的访问。在 Info.plist 中添加以下配置:

    <key>NSDesktopFolderUsageDescription</key>
    <string>需要访问桌面文件夹以选择文件</string>
    <key>NSDocumentsFolderUsageDescription</key>
    <string>需要访问文档文件夹以选择文件</string>
    
  3. 最简方式测试排查
    先注释掉自定义的 FileTypes 配置,直接调用 await FilePicker.PickAsync()。如果此时能正常选择文件,说明问题确实出在文件类型的 UTI 配置上,再针对性调整 UT 类型即可。

内容的提问来源于stack exchange,提问作者an22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 14:56:11