相关疑难解决方法(0)

c#中的异步属性

在我的Windows 8应用程序中有一个全局类,其中有一些静态属性,如:

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();
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

您可以看到我想在项目的其他位置使用应用程序根文件夹,因此我将其设置为静态属性.在getter中,我需要确保根文件夹存在,否则创建它.但这CreateFolderAsync是一个异步方法,这里我需要一个同步操作.我试过了,GetResults()但它抛出一个InvalidOperationException.什么是正确的实施?(package.appmanifest已正确配置,实际创建了该文件夹.)

.net c# asynchronous async-await

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net ×1

async-await ×1

asynchronous ×1

c# ×1