C# 非异步地反序列化文件中的数据

C# 非异步地反序列化文件中的数据,c#,serialization,windows-8,windows-runtime,isolatedstorage,C#,Serialization,Windows 8,Windows Runtime,Isolatedstorage,我只是想知道在WinRT应用程序中是否有可能不异步地反序列化文件中的数据。看起来我必须获取StorageFile对象才能对其进行反序列化,并且获取它必须是异步的。是这样吗?或者你知道我能不能同步完成? 这是可以工作的异步代码,但它是异步的 public static async Task<StorageFile> GetFileFromAsync(string relativeFileName) { StorageFolder localAppData

我只是想知道在WinRT应用程序中是否有可能不异步地反序列化文件中的数据。看起来我必须获取StorageFile对象才能对其进行反序列化,并且获取它必须是异步的。是这样吗?或者你知道我能不能同步完成? 这是可以工作的异步代码,但它是异步的

    public static async Task<StorageFile> GetFileFromAsync(string relativeFileName)
    {
        StorageFolder localAppDataFolder = ApplicationData.Current.LocalFolder;
        try
        {
            StorageFile file = await localAppDataFolder.GetFileAsync(relativeFileName);
            return file;
        }
        catch (FileNotFoundException)
        {
            return null;
        }
    }

    public static async Task<T> ReadFromXmlInIsAsync<T>(string path) where T : class
    {
        T data;
        StorageFile isolatedStorageFile = await IsolatedStorageHelper.GetFileFromAsync(path);
        if(isolatedStorageFile == null) return null;
        using (IInputStream sessionInputStream = await isolatedStorageFile.OpenReadAsync())
        {
            DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(T));
            data = (T)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
        }
        return data;
    }
公共静态异步任务GetFileFromAsync(string relativeFileName)
{
StorageFolder localAppDataFolder=ApplicationData.Current.LocalFolder;
尝试
{
StorageFile文件=等待localAppDataFolder.GetFileAsync(relativeFileName);
返回文件;
}
捕获(FileNotFoundException)
{
返回null;
}
}
公共静态异步任务ReadFromXmlInIsAsync(字符串路径),其中T:class
{
T数据;
StorageFile isolatedStorageFile=等待IsolatedStorageHelper.GetFileFromAsync(路径);
if(isolatedStorageFile==null)返回null;
使用(IInputStream sessionInputStream=await isolatedStorageFile.OpenReadAsync())
{
DataContractSerializer sessionSerializer=新的DataContractSerializer(typeof(T));
数据=(T)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
返回数据;
}

不,没有同步的方法


所有WinRT API都是异步的,以鼓励开发人员编写响应迅速的应用程序。

使用
await
关键字,它非常接近于同步的等价物。你需要同步的原因是什么?