While loading a default view is great, users might get tired of always having to change it each time they run the application. In Silverlight we can store some simple data to the isolated storage to save views across sessions. We will start by creating a default view that is persisted across sessions in isolated storage. The IsolatedStorageSettings.ApplicationSettings class allows you to save and load application settings very easily. By default, the isolated storage is limited to 1 MB, but the size of the OLAP view is not affected by this.
In this example we will save the current view in the current application’s Exit event. So any customizations made by the user are automatically saved when he closes the application and can be restored next time he runs it.
Visual Basic |
Copy Code
|
---|---|
' save the current view to storage when closing the app
Private Sub Current_Exit(sender As Object, e As EventArgs)
Dim userSettings = IsolatedStorageSettings.ApplicationSettings
userSettings(VIEWDEF_KEY) = _c1OlapPage.ViewDefinition
userSettings.Save()
End Sub
|
C# |
Copy Code
|
---|---|
// save the current view to storage when closing the app void Current_Exit(object sender, EventArgs e) { var userSettings = IsolatedStorageSettings.ApplicationSettings; userSettings[VIEWDEF_KEY] = _c1OlapPage.ViewDefinition; userSettings.Save(); } |
Notice here we access the application settings using a unique key index. We store data from the ViewDefinition property, a string in XML format which defines our view for this data set. At any point in our application we can restore the OLAP view by reversing the second line of code. Next we will load the view from isolated storage.
Visual Basic |
Copy Code
|
---|---|
Const VIEWDEF_KEY As String = "C1OlapViewDefinition"
|
C# |
Copy Code
|
---|---|
const string VIEWDEF_KEY = "C1OlapViewDefinition"; |
Add this line of code which declares our VIEWDEF_KEY constant so we can easily use the same unique key to access our stored data view throughout the application.
Application.Current.Exit += Current_Exit;
The above line of code attaches our exit event which will fire before the application closes. Next, we will load the view from isolated storage by reversing the code used to save it.
Visual Basic |
Copy Code
|
---|---|
' initialize olap view Dim userSettings = IsolatedStorageSettings.ApplicationSettings If userSettings.Contains(VIEWDEF_KEY) Then ' load last used olap view from isolated storage _c1OlapPage.ViewDefinition = TryCast(userSettings(VIEWDEF_KEY), String) End If |
C# |
Copy Code
|
---|---|
// initialize olap view var userSettings = IsolatedStorageSettings.ApplicationSettings; if (userSettings.Contains(VIEWDEF_KEY)) { // load last used olap view from isolated storage _c1OlapPage.ViewDefinition = userSettings[VIEWDEF_KEY] as string; } |
If you run the project now, you will notice that it starts with the default view created by code. If you make any changes to the view, close the application, and then re-start it, you will notice that your changes are restored.