📄 settings.cs
字号:
using System;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace MobileDevelopersHandbook
{
public class Settings
{
private static readonly String settingsFileName = "MobileDeveloperHandbookSettings.xml";
/// <summary>
/// Static private member stores ref to single instance of this class
/// </summary>
private static Settings thisClass = null;
public static Settings Instance
{
get { return Settings.GetSettings(); }
}
/// <summary>
/// Static method returns single instance of the settings class.
/// First time this method is called it will load the settings class
/// from the persistance file, if one exists.
/// </summary>
/// <returns>single instance of the Settings class</returns>
private static Settings GetSettings()
{
if (Settings.thisClass == null)
{
// Create instance of the class
Settings.thisClass = new Settings();
// Load from persistence file (if one exists)
string _path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
bool haveSettings = false;
if (File.Exists(_path + "\\" + settingsFileName))
{
XmlSerializer serializer = XmlSerializerCache.GetXmlSerializer(typeof(Settings), "http://tempuri.org/");
TextReader reader = new StreamReader(_path + "\\" + settingsFileName);
try
{
Settings myClass = (Settings)serializer.Deserialize(reader);
Settings.thisClass = myClass;
haveSettings = true;
}
catch (InvalidOperationException ex)
{
System.Windows.Forms.MessageBox.Show("Error reading Settings file: " + ex.Message);
}
reader.Close();
}
if (!haveSettings)
{
// No persistance file - initialise settings with defaults
Settings.thisClass.settingsHashTable["FontName"] = "Nina";
Settings.thisClass.settingsHashTable["FontSize"] = 11;
Settings.thisClass.settingsHashTable["BackgroundColor"] = Color.Black;
Settings.thisClass.settingsHashTable["TextColor"] = Color.White;
}
}
return Settings.thisClass;
}
/// <summary>
/// Save the settings to the persistance file
/// </summary>
public static void SaveSettings()
{
XmlSerializer serializer = XmlSerializerCache.GetXmlSerializer(typeof(Settings), "http://tempuri.org/");
string _path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
TextWriter writer = new StreamWriter(_path + "\\" + settingsFileName, false);
serializer.Serialize(writer, Settings.Instance);
writer.Close();
}
#region Methods
/// <summary>
/// default constructor for serialization
/// </summary>
public Settings()
{
}
public string SerializeColor(Color color)
{
return string.Format("{0}:{1}:{2}",
color.R, color.G, color.B);
}
public Color DeserializeColor(string color)
{
int r, g, b;
string[] pieces = color.Split(new char[] { ':' });
r = int.Parse(pieces[0]);
g = int.Parse(pieces[1]);
b = int.Parse(pieces[2]);
return Color.FromArgb(r, g, b);
}
#endregion
#region Properties
/// <summary>
/// stores the individual settings
/// </summary>
[XmlIgnore()]
private ListDictionary settingsHashTable = new ListDictionary();
/// <summary>
/// Font for text display
/// </summary>
public String Fontname
{
get
{
return (String)settingsHashTable["FontName"];
}
set
{
settingsHashTable["FontName"] = value;
}
}
/// <summary>
/// Size of font used for display
/// </summary>
public int FontSize
{
get
{
return (int)settingsHashTable["FontSize"];
}
set
{
settingsHashTable["FontSize"] = value;
}
}
/// <summary>
/// color for background
/// </summary>
[XmlIgnore()]
public System.Drawing.Color BackgroundColor
{
get
{
return (Color)settingsHashTable["BackgroundColor"];
}
set
{
settingsHashTable["BackgroundColor"] = value;
}
}
[XmlElement("BackgroundColor")]
public string XmlBackgroundColor
{
get
{
return Settings.Instance.SerializeColor(BackgroundColor);
}
set
{
BackgroundColor = Settings.Instance.DeserializeColor(value);
}
}
/// <summary>
/// color for text
/// </summary>
[XmlIgnore()]
public System.Drawing.Color TextColor
{
get
{
return (Color)settingsHashTable["TextColor"];
}
set
{
settingsHashTable["TextColor"] = value;
}
}
[XmlElement("TextColor")]
public string XmlTextColor
{
get
{
return Settings.Instance.SerializeColor(TextColor);
}
set
{
TextColor = Settings.Instance.DeserializeColor(value);
}
}
#endregion
}
public class XmlSerializerCache
{
#region fields
private static ListDictionary table;
#endregion // fields
#region constructors
static XmlSerializerCache()
{
XmlSerializerCache.table = new ListDictionary();
}
private XmlSerializerCache()
{
}
#endregion // constructors
public static XmlSerializer GetXmlSerializer(Type type, string defaultNamespace)
{
XmlSerializer serializer;
if (type == null)
{
throw new ArgumentNullException("type");
}
// Make it thread safe
lock (XmlSerializerCache.table.SyncRoot)
{
string typeName;
if ((defaultNamespace == null) || (defaultNamespace.Length == 0))
{
typeName = type.FullName + "#";
}
else
{
typeName = type.FullName + "#" + defaultNamespace;
}
// Try to get the serializer from cache
object obj = XmlSerializerCache.table[typeName];
if (obj == null)
{
// We don't have it, create a new instance
obj = new XmlSerializer(type, defaultNamespace);
XmlSerializerCache.table.Add(typeName, obj);
}
serializer = obj as XmlSerializer;
}
return serializer;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -