【发布时间】:2020-05-08 02:31:46
【问题描述】:
在我的 Windows 8 应用程序中有一个 global 类,其中有一些静态属性,例如:
public class EnvironmentEx
{
public static User CurrentUser { get; set; }
//and some other static properties
//notice this one
public static StorageFolder AppRootFolder
{
get
{
return KnownFolders.DocumentsLibrary
.CreateFolderAsync("theApp", CreationCollisionOption.OpenIfExists)
.GetResults();
}
}
}
您可以看到我想在项目的其他地方使用应用程序根文件夹,因此我将其设为静态属性。在 getter 内部,我需要确保根文件夹存在,否则创建它。但是CreateFolderAsync是一个异步方法,这里我需要一个同步操作。我尝试了GetResults(),但它抛出了InvalidOperationException。什么是正确的实现? (package.appmanifest配置正确,文件夹实际创建。)
【问题讨论】:
标签: c# .net asynchronous async-await