Windows11桌面C#中FileOpenPicker失效:Uno未实现PickMultipleFilesAsync
解决Uno平台中FileOpenPicker.PickMultipleFilesAsync未实现的问题
Uno框架目前未实现FileOpenPicker.PickMultipleFilesAsync()接口,所以在Windows窗体环境下调用该方法会抛出System.NotImplementedException。以下是两种可行的解决办法:
方案一:使用WinForms原生文件选择器
直接替换原代码为WinForms的OpenFileDialog,它支持多选文件,且适配Windows平台:
using System.Windows.Forms; using System.IO; using Windows.Storage; // 初始化WinForms文件选择器 var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "PNG图片|*.png"; // 过滤文件类型 openFileDialog.Multiselect = true; // 开启多选 openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); // 起始位置设为图片库 // 弹出选择窗口并处理结果 if (openFileDialog.ShowDialog() == DialogResult.OK) { foreach (var filePath in openFileDialog.FileNames) { // 将本地路径转为StorageFile(如需兼容原逻辑) var file = await StorageFile.GetFileFromPathAsync(filePath); // 这里写入你的文件处理逻辑 } }
注意:使用前需确保项目已引用System.Windows.Forms程序集,可通过NuGet安装该包。
方案二:跨平台条件编译适配
如果你的应用需要兼容多平台,可通过Uno的条件编译指令,针对Windows平台使用WinForms选择器,其他平台保留原Uno逻辑:
#if WINDOWS using System.Windows.Forms; using System.IO; #endif using Windows.Storage.Pickers; using Windows.Storage; #if WINDOWS // Windows平台使用WinForms原生选择器 var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "PNG图片|*.png"; openFileDialog.Multiselect = true; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); if (openFileDialog.ShowDialog() == DialogResult.OK) { foreach (var filePath in openFileDialog.FileNames) { var file = await StorageFile.GetFileFromPathAsync(filePath); // 文件处理逻辑 } } #else // 其他平台使用Uno的FileOpenPicker var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".png"); var files = await picker.PickMultipleFilesAsync(); if(files != null) { // 文件处理逻辑 } #endif
内容的提问来源于stack exchange,提问作者Whelandrew
相关产品推荐
相关产品推荐

