Thursday, September 23, 2010

WP7 Application Settings on IsolatedStorage


I'm starting to put together pieces for my WP7 application and one of the features I needed is to save/load application settings. There is already an integrated mechanism using IsolatedStorageSettings.ApplicationSettings, but I've implemented it using an XML file located on the IsolatedStorage. The main reason why I don't use the default one is because I want to upload/backup the file to DropBox or SkyDrive making it possible to share the same settings between various versions of the same application running on different platforms (in my case iOS and WP7) and also the user can restore the settings of an application when he changes the phone or reinstalls the application.

The VS2010 solution I am posting at the end of this entry is structured in 3 projects:

1. ConfigManager is the library used for settings management. It can read/write the settings. It also implements an indexed property, but as WP7 v1 uses Silverlight 3 it's not possible to use bindind to indexed properties (this feature is only available from Silverlight 4). If you want to directly bind settings to UI elements you will have to create bind-able properties . If the configuration file is not found on the IsolatedStorage the library will try to restore the file from Content (have a look at the sample TestConfig). It is possible to manually add values to the settings. There is nothing extraordinary in the source code.
2. TestConfig is a sample project that uses the library ConfigManager. It has a default configuration file deployed in Content (settings.xml). One of the things to pay attention to if you are binding to textboxes is to be sure that the binding is updated before saving the settings (this because if a textbox has focus, you modify it's value and select save settings you will loose the modifications if the binding is not updated). This is why I force the update of the source on TextChanged event:
((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
not a very elegant solution but it's the only one I found till now.
3. TestConfigMVVM is the same test project using MVVM light library. I did not include a settings file in the content and I load the default values manually.

Hope you will find the library useful and that you could spare the time I've spent in developing this part. The next post will be on synchronization with DropBox.



NAMASTE

No comments:

Post a Comment