【发布时间】: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