我正在尝试使用MVIMLight中的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>();
答案 0 :(得分:0)
界面:
public interface ISerialisationService
{
Task<T> LoadLocalXMLAsync<T>(string filename);
Task SaveLocalXMLAsync(string filename, object o);
}
实施:
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
}
}
}