Data Persistence Navigation in Silverlight 3

There are situations where you need to input some data as well as navigate to a specific page and come back to enter data again. In that case usually Silverlight doesn't persist data.


Introduction

There are situations where you need to input some data as well as navigate to a specific page and come back to enter data again. In that case usually Silverlight doesn't persist data.

In this article we will how can we do that.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as DataPersistence.

DataPersist1.gif

I have added some controls for the Home Page in Expression Blend.

DataPersist2.gif

As you see above, we have normal input scenario with TextBox, Radio Buttons and ComboBox.

Now we would create a class which will contain our persist properties.

public class Person
    {
        public string Name { get; set; }
        public string EmailId { get; set; }
        public string UserType { get; set; }
        public string Country { get; set; }
    }

We need to store data globally, so we would create a global property in App.xaml.cs.

public object PersistedData { get; set; }

In the Button Click of About we would navigate to About Page. So during that event we need to store data.
The following code is for persisting data:

private void btnAbout_Click(object sender, RoutedEventArgs e)
        {
            Person toPersist = new Person();
            toPersist.Name = txtName.Text;
            toPersist.EmailId = txtEmailId.Text;
            if (radioAdmin.IsChecked == true)
                toPersist.UserType = "Admin User";
            else if (radioUser.IsChecked == true)
                toPersist.UserType = "Normal User";

            if (cmbCountry.SelectedIndex != -1)
                toPersist.Country = cmbCountry.SelectionBoxItem.ToString();

            ((App)Application.Current).PersistedData = toPersist;
 
            NavigationService.Navigate(new Uri("/About", UriKind.RelativeOrAbsolute));
}


Now we would come back to our home page and restore the persisted data.

When you come back to any navigation page, the OnNavigatedTo event is fired. So we would check there.
Add the following code in OnNavigatedTo:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Person persisted = ((App)Application.Current).PersistedData as Person;
            if (persisted != null)
            {
                txtName.Text = persisted.Name;
                txtEmailId.Text = persisted.EmailId;
                if (persisted.UserType == "Admin User")
                    radioAdmin.IsChecked = true;
                else if (persisted.UserType == "Normal User")
                    radioUser.IsChecked = true;
                foreach (ComboBoxItem item in cmbCountry.Items)
                {
                    if (item.Content.Equals(persisted.Country))
                        cmbCountry.SelectedItem = item;
                }
            }
 
            ((App)Application.Current).PersistedData = null;

        }

That's it. Now we would test our application.

DataPersist3.gif

Now click on About and you will be navigated to About page.

DataPersist4.gif
Now come back to the page by clicking on the Home Link on top right.

DataPersist5.gif
And data is persisted. That is so simple, isn't it. Hope this article helps.