Generalized settings UI + settings classes
For more classes derived from a base class I decided to have a base UserControl and derived ones for each derived class (for visualisation; box on the right side of the image).
Now each of the derived UserControls have a specific settings (red rectangle on left) The settings UI is a separate UserControl (can be moved freely, put to separate dialog if too big...) and has a underlying data class which it changes (instance of this class is member of the main UserControl).
alt text http://a.imageshack.us/img651/2710/46937664.png
This brings me to creating a separate class holding (and possibly saving) the settings data, separate UserControl to set the values and connecting it manually all together.
This is not the first time I run into similar problem so I started to think about some class +interface allowing me to easily create the data holding classes and have the user controls generated. My idea is something likeclass SettingsItem
{
string name;
TypeEnum type;
double value; //how about different types eg. bool? Inheritance?
}
The settings class would be a List of these items. For each type there would be a single value UserControl (checkbox/textbox/numericfield + description). A foreach
loop would than create the final UserControl (one value UCs one under each other).
On one hand I like this idea as an intriguing challenge and potential开发者_如何学编程 relief of boring, repetitive and time consuming putting smaller squares into bigger ones and aligning them. On the other hand it seems to me a bit like killing a fly with a shotgun.
Is there some standard/ready solution to this? Any better way to do it? Or is it just too complex solution to minor problem? I'll be happy to see any answers, suggestions, remarks...
First of all, take a look at this thread, it might discover interesting things for you.
Anyway, per your question, you could use serialization for this purpose, if for some reason you don't want to use the .settings file feature provided with VS, here is how you convert and unconvert your items to XML:
class Program
{
static void Main(string[] args)
{
var setting = new SettingsItem
{
Name = "Setting1",
Type = SettingTypes.UserSetting,
Value = "setting"
};
XmlSerializer serializer = new XmlSerializer(typeof(SettingsItem));
StringBuilder sb = new StringBuilder();
using (StringWriter sr = new StringWriter(sb))
serializer.Serialize(sr, setting);
var serialized = sb.ToString();
Console.WriteLine(serialized);
SettingsItem item;
using (var r = new StringReader(sb.ToString()))
item = (SettingsItem)serializer.Deserialize(r);
Console.WriteLine("Name: {0}, Type: {1}, Value: {2}",
item.Name,
item.Type,
item.Value);
Console.ReadKey();
}
}
[Serializable]
public class SettingsItem
{
public string Name { get; set; }
public SettingTypes Type { get; set; }
public object Value { get; set; }
}
[Flags]
public enum SettingTypes
{
ApplicationSetting = 1,
UserSetting = 2,
AdminOnly = 4,
Temporary = 8
}
Once you get the XML simply store in the ApplicationData folder or anywhere else you'd like.
精彩评论