我是跨平台C#应用程序的开发人员,该应用程序当前针对Windows Store和Xamarin环境。我有一个由所有应用程序引用的共享可移植类库(PCL),其中定义了核心应用程序逻辑和算法,包括存储,网络,数据库和设置。
我需要的是一种更智能的存储设置方式。我不想使用任何键值方法或XML文件,因为这些方法已完全过时,因此在C#中很容易实现:
[SettingsNode("app_settings")]
class AppSettings
{
public double SomeNumber;
public object SomeSerializable;
public byte[] SomeBytes;
[SettingsNode("custom_setting_name")]//just specifying XML/etc node name with SettingsNode attribute
public string SomeKey;
}
class x
{
void y()
{
AppSettings superSettingsSolution = Engine.LoadEntry<AppSettings>("test.xml");
superSettingsSolution.SomeBytes = new byte[10];
superSettingsSolution.SomeNumber = 4.6d;
superSettingsSolution.SomeSerializable = new object();//[Serializable] class
Engine.SaveEntry<AppSettings>("test.xml", superSettingsSolution);
}
}
我将使用PCLStorage包进行跨平台存储,因此我没有在代码中引用System.Windows.Storage,而且我也不想这样做。
我的问题:C#的PCL的代码示例库/项目/程序包附近有什么吗?对不起,如果我要问些琐碎的事情,但是我还没有使用“便携式” / windows8程序员。
using System.Xml.Serialization;
using PCLStorage;
using System.IO;
public virtual async Task<T> LoadSettings<T>(IFile file = null)
where T : IApplicationSettings
{
// File
if (file == null)
file = DefaultSettingsFile;
// Open file
using (Stream fileStream = await file.OpenAsync(FileAccess.Read))
{
var xmls = new XmlSerializer(typeof(T));
return (T)xmls.Deserialize(fileStream);
}
}
public virtual async Task SaveSettings(object settings, IFile file = null)
{
// File
if (file == null)
file = DefaultSettingsFile;
// Open file
using (Stream fileStream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
var xmls = new XmlSerializer(settings.GetType());
xmls.Serialize(fileStream, settings);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句