I have a WPF desktop app and I want to use the UWP FolderPicker API to pick a directory. My app uses the UWP packaging project so it is built and ran as an appx. I've added in the Windows and WindowsBase references and my project builds and runs. However I get a runtime error when trying to use the folder picker. My code is as follows:
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
{
}
}
The error I get is on the line System.Exception: await folderPicker.PickSingleFolderAsync();
and the error 'Invalid window handle. (Exception from HRESULT: 0x80070578)'
What am I missing or is it even possible to use the FolderPicker from a WPF app?