暂时不要放弃 app.config/app-settings。
为了在我们的应用程序配置中存储更复杂的数据结构,我们采用了两种方法:如果我们尝试存储的数据结构可以序列化到 XML 或从 XML 序列化,我们将其作为字符串存储在应用程序设置中.另一种方法是实现TypeConverter,它将您的数据结构转换为字符串并返回。
这是一个裁剪的示例:
[TypeConverter(typeof(FormStateConverter))]
public class FormState : INotifyPropertyChanged, IDisposable {
private Size _Size = Size.Empty;
private Point _Location = Point.Empty;
private FormWindowState _WindowState = FormWindowState.Normal;
public FormState(Form form) { BindTo(form); }
internal FormState(Size size, Point location, FormWindowState state) {
_Size = size;
_Location = location;
_WindowState = state;
}
// lotsa other code...
}
internal class FormStateConverter : ExpandableObjectConverter {
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(string)) {
return true;
} else {
return base.CanConvertFrom(context, destinationType);
}
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
} else {
return base.CanConvertFrom(context, sourceType);
}
}
// This converts a FormState to a string, we're just making a CSV string here...
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == typeof(String)) {
FormState formState = (FormState)value;
string converted = string.Format("{0},{1},{2},{3},{4}", formState.Size.Height, formState.Size.Width,
formState.Location.X, formState.Location.Y, formState.WindowState.ToString());
return converted;
}
return base.ConvertTo(context, culture, value, destinationType);
}
// This converts a string back into a FormState instance.
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string formStateString = (string)value;
string[] parts = formStateString.Split(','); // split the CSV string
if (parts != null && parts.Length == 5) { // attempt some error checking
Size size = new Size();
Point location = new Point();
FormWindowState state = FormWindowState.Normal;
int tmp;
size.Height = (Int32.TryParse(parts[0], out tmp)) ? tmp : 0;
size.Width = (Int32.TryParse(parts[1], out tmp)) ? tmp : 0;
location.X = (Int32.TryParse(parts[2], out tmp)) ? tmp : 0;
location.Y = (Int32.TryParse(parts[3], out tmp)) ? tmp : 0;
if (string.Equals(parts[4], "maximized", StringComparison.OrdinalIgnoreCase)) {
state = FormWindowState.Maximized;
} else if (string.Equals(parts[4], "minimized", StringComparison.OrdinalIgnoreCase)) {
state = FormWindowState.Minimized;
} else {
state = FormWindowState.Normal;
}
return new FormState(size, location, state);
}
}
return base.ConvertFrom(context, culture, value);
}
}
在实现类型转换器并使用TypeConverterAttribute 赋予我们的FormState 数据类型后,FormState 类型将显示在 Visual Studio 的设置设计器中: