【问题标题】:Hololens 2 - Impossible to access file in ApplicationData.Current.RoamingFolderHololens 2 - 无法访问 ApplicationData.Current.RoamingFolder 中的文件
【发布时间】:2022-11-11 03:20:29
【问题描述】:

我是 Hololens 2 编程的新手。我正在使用 Unity for Holo2 开发 UWP 应用程序,该应用程序使用 XML 配置文件来接收有关将 3D 对象放置在与标记的相对位置的信息。当我尝试从 Resources 文件夹(Unity 和 Hololens)和 PC AppData(Unity)读取和处理文件时,它工作正常,但是当我尝试从 Hololens AppData 文件夹中读取文件时(也当我尝试从特殊文件夹 KnownFolders 中读取文件)。 我使用“ApplicationData.Current.RoamingFolder.Path”作为内部 UWP 文件夹(可从 DevicePortal 访问),使用 StorageFolder 和 StorageFile 作为新任务中的 await Get async 方法。 我还用正确的 FileTypeAssociation 为 .xml 修改了 package.appxmanifest 的代码 我希望在 ApplicationData.Current.RoamingFolder.Path 的路径中用作用户名的 Microsoft 帐户电子邮件 (user@mail.com) 不是异步方法的问题。

//...
using System.Xml.Linq;
using System.Threading.Tasks;
//...

#if WINDOWS_UWP
using Windows.Storage;
#endif

这里是流的加载

#if WINDOWS_UWP      
        try
        {
            folderPathName = ApplicationData.Current.RoamingFolder.Path;
            using (Stream s = openFileUWP(folderPathName, filenameWithExtension)) 
            {
                document = XDocument.Load(s);
            }
        }
        catch (Exception e)
        {
            document = XDocument.Parse(targetFile.text); //the XML file in Resources folder
        }
#else
        //...
#endif

这里是openFileUWP函数

#if WINDOWS_UWP
    private Stream openFileUWP(string folderName, string fileName)
    {
        Stream stream = null;

        Task task = new Task(
                        async () =>
                        {
                            StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
                            StorageFile file = await folder.GetFileAsync(fileName);  
                            stream = await file.OpenStreamForReadAsync(); 
                        });
        
        task.Start();
        task.Wait();            

        return stream;
    }
#endif

【问题讨论】:

    标签: c# unity3d async-await uwp hololens


    【解决方案1】:

    您正在使用 Task 构造函数,建议不要这样做。见Task Constructors

    此外,Task 构造函数采用Action。因此,没有要等待的任务,当任务开始时,它几乎立即完成。

    此外,阻塞可能会阻塞甚至死锁您的 UI。

    尝试这个:

    private async Tasl<Stream> openFileUWP(string folderName, string filename)
    {
        var folder = await StorageFolder.GetFolderFromPathAsync(folderName);
        var file = await folder.GetFileAsync(filename);  
        var stream = await file.OpenStreamForReadAsync(); 
        return stream;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2017-12-11
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2018-01-31
      • 2021-01-09
      • 2018-05-22
      • 1970-01-01
      相关资源
      最近更新 更多