首页 文章

如何在WPF c#桌面应用程序中使用UWP FolderPicker

提问于
浏览
1

我有一个WPF桌面应用程序,我想使用UWP FolderPicker API来选择一个目录 . 我的应用程序使用UWP打包项目,因此它是作为appx构建和运行的 . 我添加了Windows和WindowsBase引用,我的项目构建和运行 . 但是,在尝试使用文件夹选择器时出现运行时错误 . 我的代码如下:

private async void OnGetDirectory(object parameter)
    {

        var folderPicker = new Windows.Storage.Pickers.FolderPicker();
        folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add("*");

        Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        if (folder != null)
        {
            // Application now has read/write access to all contents in the picked folder
            // (including other sub-folder contents)
            Windows.Storage.AccessCache.StorageApplicationPermissions.
            FutureAccessList.AddOrReplace("PickedFolderToken", folder);
        }
        else
        {
        }
    }

我得到的错误是在System.Exception行上: await folderPicker.PickSingleFolderAsync(); 和错误' Invalid window handle. (Exception from HRESULT: 0x80070578)'

我缺少什么,甚至可以使用WPF应用程序中的FolderPicker?

2 回答

  • 0

    根据UWP APIs available to a packaged desktop app (Desktop Bridge)文章,一些功能区域尚未经过全面测试或目前正在按预期运行 . 您在列表中使用的"File and folder pickers"功能 . 该功能的详细说明如下:

    打包的应用程序具有完整的文件系统访问权限,不需要UWP选择器 .

    因此,您的UWP包装项目可能无法完成 FolderPicker . 请尝试使用WPF本身支持的文件相关API .

    更多细节请参考this document .

  • 1

    您可以在 System.Windows.Forms 命名空间中使用正常的Windows窗体文件夹对话框( FolderBrowserDialog )来代替UWP FolderPicker (例如,请参阅this question) .

    如果要使用 StorageFolder ,可以调用 StorageFolder.GetFolderFromPathAsync 方法,也可以使用经典文件夹API,如 System.IO.DirectorySystem.IO.DirectoryInfo .

相关问题