任务的界面帮助<方法
本文关键字:方法 帮助 界面 任务 | 更新日期: 2023-09-27 17:53:55
我试图解耦一些具体的类和链接使用SimpleIOC从MVVMLight。这是我的类,我想做一个接口,然后我可以注册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
}
}
}
我已经尝试了一些东西,不能工作出我如何定义一个通用接口返回任务在这种情况下。
我需要使用
注册SimpleIoc.Default.Register<ISerialisationService, SerialisationService>();
The Interface:
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
}
}
}