Skip to main content

Persistence of Wave Manager user's preferences to an XML file

Steve asks:
I figured out how to set the background, foreground etc to the user setting config file but I am having trouble saving the opened children widows on closing to the settings. Is it approprate to save this info to the user setting config file?
I create a new setting of type String Collection but at start-up when reading the user setting file it is extreamly slow. Would I be better off in the registry.

Youssef's Reply:
Steve, you have several choices but I believe you may be making it more difficult that it needs to be. Putting some configuration in an xml/config file and the rest in the settings file is not acceptable. The Settings even though may seem straight forward is not the right place and will most likely cause you more grief than it's worth.
The choices you had are:
1- Simplest:
1a- Make a UserPreferences class that conatins all the persistent information (WaveBackColor, WaveForeColor, WaveThickness, FileViewFont, etc., and what you need is a StringCollection (like a List) for the open document files and you need a Rectangle which represents the Main Window dimensions and location).
1b- Build a UserPreferencesMgr static class that contains a single static member of type UserPreferences.
1c- When the user changes a color, font, thickness, etc, Simply use the UserPreferencesMgr to set those values into the fields of the UserPreferences class.
1d- Once you have that, all you need to do is Within the Main Form's Closing event, set the Window Rectangle in UserPreferences to that of your main Form Window and the list of Open MDI Child wave files.
1e- Within the Main Form's Closing event Then Serialize that UserPreferences class using SoapFormatter (SOAP = XML, ugly XML but XML nonetheless). This is the easiest choice.
2- A bit more Complex but cleaner:
2a- Same as 1a-1d.
2b- But instead of serializing the UserPreferences using SOA, write the values of each of the fields one by one to an XML file using an XmlWriter class or one of its cousins (See FileIO slides)
I just took the final project source I have and spent 15 minutes and switched from registry to an xml file using approach #1 above. I have uploaded the sample(version 1.0.0.7) and XML file.
Hope this helps

Comments

  1. Note that if you are using SoapFormatter, this formatter has a bug that has been (by design) outstanding for 5 years, and when I contacted Microsoft about it 5 years ago they said, they are not going to fix because SoapFormatter will no longer be supported moving forward. So SoapFOrmatter does not know how to serialize any Generic type like List for example. So instead you can use StringCollection.

    ReplyDelete

Post a Comment

Popular posts from this blog

Visual Studio 2010 was released yesterday

Unfortunately, I have just checked the MSDNAA site and they still do not have the release version of Visual Studio 2010. They only have an old Beta2. I would recommend that if you do not have access to Visual Studio 2010 through other means, that you keep an eye on the MSDNAA web site and see if VS2010 becomes available. If and when it becomes available, I would not recommend that you install it while you are finishing CS 503. Download it and save it somewhere and when the course is done, you can knock yourself out :)

Wave Undo

To implement Undo, the simplest way is to use a MemoryStream object in your WaveDisplayForm (MDI Child) and simply serialize the Wave (which should be marked as [Serializable] to the stream and hold on to the stream object. private MemoryStream _undoStream = new MemoryStream(); private bool _canUndo = false; public void SaveForUndo() {      BinaryFormatter bf = new BinaryFormatter();      bf.Serialize(_undoStream, wave);      _undoStream.Flush();      // This rewinds the stream so that when it is used for deserialization, it's ready to use,     // otherwise, the deserialization will start deserializing from the end of the stream and would fail.      _undoStream.Position = 0;      _canUndo = true; }   public void Undo() {      BinaryFormatter bf = new BinaryFormatter();      ...

My Plans for this Blog

I intend to post new things I learn or run into or that come to mind on at least a weekly basis. Sometimes may be daily. I'm planning on starting after the course ends so I do disturb the peace of the final project. You have options to unsubscribe yourself from this Blog site completely and you are welcome to stay as you fell like it. It's all up to you. I also would be open and willing to respond to those tough questions you may run into assuming the volume of questions would be manageable...... Knowing you guys, I think it will be very quite in this neighborhood.