在 Windows 8.1 通用应用程序启动时加载 xml 文件
Load xml file on start of Windows 8.1 Universal App
在我的 Windows 8.1 通用应用程序中,我想在应用程序启动时检索本地 XML 文件。但我对异步/等待感到困惑。我使用 GetFileAsync,因此它需要在异步函数中。但是我无法从应用程序的初始化中调用它。当我调用该函数时,它说我需要添加 await。如果我等待,它说我只能在异步方法中使用等待,因此我不能在初始化时使用它。那么这怎么可能呢?
非常感谢!
看这里:How to call asynchronous method from synchronous method in C#?
"If you have a simple asynchronous method that doesn't need to synchronize back to its context, then you can use Task.WaitAndUnwrapException"
所以将代码放到一个单独的方法中,使用上面的方法或者第二种方法RunTask。
在我的 Windows 8.1 通用应用程序中,我想在应用程序启动时检索本地 XML 文件。但我对异步/等待感到困惑。我使用 GetFileAsync,因此它需要在异步函数中。但是我无法从应用程序的初始化中调用它。当我调用该函数时,它说我需要添加 await。如果我等待,它说我只能在异步方法中使用等待,因此我不能在初始化时使用它。那么这怎么可能呢? 非常感谢!
看这里:How to call asynchronous method from synchronous method in C#?
"If you have a simple asynchronous method that doesn't need to synchronize back to its context, then you can use Task.WaitAndUnwrapException"
所以将代码放到一个单独的方法中,使用上面的方法或者第二种方法RunTask。