0

我正在尝试使用 MVVMLight 中的 SimpleIOC 解耦一些具体的类和链接。这是我的课,我想制作一个接口,然后可以向 simpleioc 注册

public class SerialisationService : ISerialisationService
{

    private static async Task<T> LoadLocalXMLAsync<T>(string filename)
    {
        try
        {
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            T o = default(T);
            using (IInputStream inStream = await file.OpenSequentialReadAsync())
            {
                var serializer = new DataContractSerializer(typeof(T));
                o = (T)serializer.ReadObject(inStream.AsStreamForRead());
            }
            return o;
        }
        catch (Exception ex)
        {
            return default(T);
            // ERROR HANDLING AND LOGGING
        }
    }

    private async static Task SaveLocalXMLAsync(string filename, object o)
    {
        try
        {
            var serializer = new DataContractSerializer(o.GetType());
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            using (Stream fileStream = await file.OpenStreamForWriteAsync())
            {
                serializer.WriteObject(fileStream, o);
                await fileStream.FlushAsync();
            }
        }
        catch (Exception ex)
        {
            // ERROR HANDLING AND LOGGING
        }
    }

}

我已经尝试了一些事情,但无法弄清楚在这种情况下如何定义用于返回 Task 的通用接口。

我需要使用注册

SimpleIoc.Default.Register<ISerialisationService, SerialisationService>();
4

1 回答 1

0

The Interface:

public interface ISerialisationService
{
    Task<T> LoadLocalXMLAsync<T>(string filename);
    Task SaveLocalXMLAsync(string filename, object o);
}

The Implementation:

public class SerialisationService : ISerialisationService
{

    public async Task<T> LoadLocalXMLAsync<T>(string filename)
    {
        try
        {
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            T o = default(T);
            using (IInputStream inStream = await file.OpenSequentialReadAsync())
            {
                var serializer = new DataContractSerializer(typeof(T));
                o = (T)serializer.ReadObject(inStream.AsStreamForRead());
            }
            return o;
        }
        catch (Exception ex)
        {
            return default(T);
            // ERROR HANDLING AND LOGGING
        }
    }

    public async Task SaveLocalXMLAsync(string filename, object o)
    {
        try
        {
            var serializer = new DataContractSerializer(o.GetType());
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            using (Stream fileStream = await file.OpenStreamForWriteAsync())
            {
                serializer.WriteObject(fileStream, o);
                await fileStream.FlushAsync();
            }
        }
        catch (Exception ex)
        {
            // ERROR HANDLING AND LOGGING
        }
    }

}
于 2013-01-17T19:28:50.593 回答