在Hololens上评估OpenFilePicker时调用System.Exception [英] System.Exception getting called when assessing OpenFilePicker on Hololens

查看:90
本文介绍了在Hololens上评估OpenFilePicker时调用System.Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为hololens应用程序使用MRTK,我需要选择用户放入其文档文件夹中的文件.我正在尝试访问FileOpenPicker,并通过按一下按钮使用PickSingleFileAsync()函数来获取文件,然后将其加载到我的应用程序中.

I'm using the MRTK for a hololens app and I need to select a file that the user puts in their document folder. I am trying to access the FileOpenPicker and use the PickSingleFileAsync() function from a button press to get the file and then load that into my app.

此函数中的代码基本上就是我正在做的事情:

The code inside this function is basically what I am doing:

(代码源)

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时,当我按下按钮时,它会顺利运行代码的第一部分,但是我在这行上遇到了异常

However, when I build and deploy to the Hololens Emulator, when I press the button It runs through the first part of the code just fine but I get an exception on this line

StorageFile文件=等待openPicker.PickSingleFileAsync();

经过广泛的研究和一些挫折之后,我对此发表了非常贫穷和模糊的文章

After extensive research and some frustration I made a very poor and vague post about it here.

在那篇文章中,我引用了这则2年前的帖子并说您无法执行此操作,但针对Hololens的Microsoft文档说,您可以使用文件选择器,在这种情况下,可以使用FileOpenPicker.

In that post I referenced this post which was made 2 years ago and says you can't do this but The Microsoft docs for Hololens say that you can use File pickers, in this case, FileOpenPicker.

我发现帖子埋在Windows Mixed中与Reality Developer Forum相关但不是我遇到的问题,我仍然觉得有必要将其包含在这篇文章中.

I found this post buried in the Windows Mixed Reality Developer Forum that's related but isn't the issue I am having, I still felt it was necessary to include in this post.

我还想补充一点,我确实安装了文件选择器应用程序.根据这篇关于Microsoft Docs的帖子如果您调用FileOpenPicker,它将打开设备上首次安装的任何文件选择器.

I also want to add that I do have a file picker app installed. According to this post on Microsoft Docs if you call FileOpenPicker it will open whatever file picker was first installed on your device.

还在正在生成的MRTK和Appx中,确保对"PictureLibrary"的许可功能已启用.

Also in the MRTK and the Appx that is being generated I am ensuring the permission to "PictureLibrary" capability is enabled.

任何帮助将不胜感激,我觉得我已经等待了太长时间,无法就该主题发表更正式的文章,并且希望能得到一些答案.谢谢!

Any help would be greatly appreciated, I feel I've waited too long to make a more formal post on this topic and I'm hoping to have some answers. Thanks!

推荐答案

感谢所有此帖子!我终于找到了解决我问题的方法.我浏览过的所有文档中都没有提到的最大调用是此 UnityEngine.WSA.Application.InvokeOnUIThread()

ALL THANKS TO THIS POST! I finally found a solution to my issue. The biggest call that wasn't mentioned in any docs I looked through was this UnityEngine.WSA.Application.InvokeOnUIThread()

我没有遇到这个问题,也没有找到任何关于这是MRTK + hololens开发统一性的解决方案的文档.因此,对于任何寻求解决该问题的人,我都会在上面引用该链接,以防将来被破坏.

I didn't run into this, nor did I find any documentation on this being a solution for MRTK+unity for hololens development. So for anyone out there looking for a solution to the issue I will quote that link above in case it's broken in the future.

#if !UNITY_EDITOR && UNITY_WSA_10_0
        Debug.Log("***********************************");
        Debug.Log("File Picker start.");
        Debug.Log("***********************************");

        UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
        {
            var filepicker = new FileOpenPicker();
            // filepicker.FileTypeFilter.Add("*");
            filepicker.FileTypeFilter.Add(".txt");

            var file = await filepicker.PickSingleFileAsync();
            UnityEngine.WSA.Application.InvokeOnAppThread(() => 
            {
                Debug.Log("***********************************");
                string name = (file != null) ? file.Name : "No data";
                Debug.Log("Name: " + name);
                Debug.Log("***********************************");
                string path = (file != null) ? file.Path : "No data";
                Debug.Log("Path: " + path);
                Debug.Log("***********************************");

                

                //This section of code reads through the file (and is covered in the link)
                // but if you want to make your own parcing function you can 
                // ReadTextFile(path);
                //StartCoroutine(ReadTextFileCoroutine(path));

            }, false);
        }, false);

        
        Debug.Log("***********************************");
        Debug.Log("File Picker end.");
        Debug.Log("***********************************");
#endif

这篇关于在Hololens上评估OpenFilePicker时调用System.Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆