c# - How can I implement a class that can be saved in User Settings? -
i have following class -
[serializable] public class soundviewmodel : inotifypropertychanged { public event propertychangedeventhandler propertychanged; public string name { get; private set; } public soundviewmodel( string name ) { this.name = name; } private fileinfo _ringin = new fileinfo( "sounds/ringin.wav" ), _correct = new fileinfo( "sounds/correct.wav" ), _incorrect = new fileinfo( "sounds/incorrect.wav" ), _timeover = new fileinfo( "sounds/timeover.wav" ); public fileinfo ringin { { return this._ringin; } set { this._ringin = value; this.onpropertychanged( "ringin" ); } } public fileinfo correct { { return this._correct; } set { this._correct = value; this.onpropertychanged( "correct" ); } } public fileinfo incorrect { { return this._incorrect; } set { this._incorrect = value; this.onpropertychanged( "incorrect" ); } } public fileinfo timeover { { return this._timeover; } set { this._timeover = value; this.onpropertychanged( "timeover" ); } } private void onpropertychanged( string p ) { if ( this.propertychanged != null ) this.propertychanged( this, new propertychangedeventargs( p ) ); } } i need save class user settings of application , here why (please not "why not make separate fileinfo entry in user settings each file in class?") : need hold 51 individual entries of class (one behave default, , 50 more; 1 each player, , not making 204 entries settings.
i did quick search on how make class can serialized , msdn article cam across said
the easiest way make class serializable mark serializable attribute
clearly, not can done 'easy way'.
this code entry (and respective list of these entries) have within user settings class -
xml -
<setting name="defaultsounds" type="eqlcontrols.controls.soundviewmodel" scope="user"> <value profile="(default)" /> </setting> <setting name="playersounds" type="system.collections.objectmodel.observablecollection<eqlcontrols.controls.soundviewmodel>" scope="user"> <value profile="(default)" /> </setting> c# -
[global::system.configuration.userscopedsettingattribute()] [global::system.diagnostics.debuggernonusercodeattribute()] public global::eqlcontrols.controls.soundviewmodel defaultsounds { { return ((global::eqlcontrols.controls.soundviewmodel)(this["defaultsounds"])); } set { this["defaultsounds"] = value; } } [global::system.configuration.userscopedsettingattribute()] [global::system.diagnostics.debuggernonusercodeattribute()] public global::system.collections.objectmodel.observablecollection<eqlcontrols.controls.soundviewmodel> playersounds { { return ((global::system.collections.objectmodel.observablecollection<eqlcontrols.controls.soundviewmodel>)(this["playersounds"])); } set { this["playersounds"] = value; } } i can provide more details if necessary.
how implement soundviewmodel class such when call settings.default.save(), settings saved?
also - 1 clue found when tried save, got within user settings file :
<eqlcontrols.properties.settings> <setting name="defaultsounds" serializeas="xml"> <value /> </setting> <setting name="playersounds" serializeas="xml"> <value /> </setting> </eqlcontrols.properties.settings> edit
it suggested use file names instead of fileinfo object. tried did not work - (i left properties fileinfo , stored file names in variables, first shot, next instead try same both strings).
public class soundviewmodel : inotifypropertychanged { public event propertychangedeventhandler propertychanged; public string name { get; private set; } public soundviewmodel( string name ) { this.name = name; } private string _ringin = "sounds/ringin.wav", _correct = "sounds/correct.wav", _incorrect = "sounds/incorrect.wav", _timeover = "sounds/timeover.wav"; public fileinfo ringin { { return new fileinfo(this._ringin ?? "sounds/ringin.wav"); } set { this._ringin = value.fullname; this.onpropertychanged( "ringin" ); } } public fileinfo correct { { return new fileinfo(this._correct ?? "sounds/correct.wav"); } set { this._correct = value.fullname; this.onpropertychanged( "correct" ); } } public fileinfo incorrect { { return new fileinfo(this._incorrect ?? "sounds/incorrect.wav"); } set { this._incorrect = value.fullname; this.onpropertychanged( "incorrect" ); } } public fileinfo timeover { { return new fileinfo(this._timeover ?? "sounds/timeover.wav"); } set { this._timeover = value.fullname; this.onpropertychanged( "timeover" ); } } private void onpropertychanged( string p ) { if ( this.propertychanged != null ) this.propertychanged( this, new propertychangedeventargs( p ) ); } } edit 2
i have removed fileinfo properties , replaced strings :
public class soundviewmodel : inotifypropertychanged { public event propertychangedeventhandler propertychanged; public string name { get; private set; } public soundviewmodel( string name ) { this.name = name; } private string _ringin = "sounds/ringin.wav", _correct = "sounds/correct.wav", _incorrect = "sounds/incorrect.wav", _timeover = "sounds/timeover.wav"; public string ringin { { return this._ringin; } set { this._ringin = value; this.onpropertychanged( "ringin" ); } } public string correct { { return this._correct; } set { this._correct = value; this.onpropertychanged( "correct" ); } } public string incorrect { { return this._incorrect; } set { this._incorrect = value; this.onpropertychanged( "incorrect" ); } } public string timeover { { return this._timeover; } set { this._timeover = value; this.onpropertychanged( "timeover" ); } } private void onpropertychanged( string p ) { if ( this.propertychanged != null ) this.propertychanged( this, new propertychangedeventargs( p ) ); } } still no love, saved user.config file barren.
<eqlcontrols.properties.settings> <setting name="playersounds" serializeas="xml"> <value /> </setting> </eqlcontrols.properties.settings>
soundviewmodel needs parameterless constructor deserialization. public properties serialized. can skip properties using [xmlignore].
you can't save generic types in user settings (observable<t> in case). there's easy work-around though , dictionaries.
[serializable] public class myserializablecollection : list<soundviewmodel>{}
Comments
Post a Comment