我正在将 MRTK 用于 hololens 应用程序,我需要选择用户放入其文档文件夹中的文件。我正在尝试访问 FileOpenPicker 并通过按下按钮使用 PickSingleFileAsync() 函数来获取文件,然后将其加载到我的应用程序中。
这个函数里面的代码基本上就是我在做的:
private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
{
// Clear previous returned file name, if it exists, between iterations of this scenario
OutputTextBlock.Text = "";
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
OutputTextBlock.Text = "Picked photo: " + file.Name;
}
else
{
OutputTextBlock.Text = "Operation cancelled.";
}
}
但是,当我构建并部署到 Hololens Emulator 时,当我按下按钮时,它会很好地运行代码的第一部分,但在这一行出现异常
StorageFile file = await openPicker.PickSingleFileAsync();
经过广泛的研究和一些挫折后,我在这里发表了一篇非常糟糕和模糊的帖子。
在那篇文章中,我引用了 2 年前发表的这篇文章,并说你不能这样做,但Hololens 的 Microsoft 文档说你可以使用文件选择器,在这种情况下是 FileOpenPicker。
我在 Windows Mixed Reality 开发人员论坛中发现了这篇文章,虽然这不是我遇到的问题,但我仍然觉得有必要将其包含在这篇文章中。
我还想补充一点,我确实安装了一个文件选择器应用程序。根据Microsoft Docs 上的这篇文章,如果您调用 FileOpenPicker,它将打开您设备上首次安装的任何文件选择器。
同样在 MRTK 和正在生成的 Appx 中,我确保启用“PictureLibrary”功能的权限。
任何帮助将不胜感激,我觉得我已经等了太久才能就这个主题发表更正式的帖子,我希望能得到一些答案。谢谢!