Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Thursday, July 8, 2010

Restoring a Windows State When Navigating Back to a Page

When navigating to another page and back again using the Silverlight Navigation Framework, sometimes it is useful to return to the page and see it exactly as it was.  Fortunately, this is easy to do if you are using an MVVM framework. 

When navigating away from the page, save the ViewModel in the App.Current.Resources collection.


protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (App.Current.Resources["theViewModel"] != null)
App.Current.Resources.Remove("theViewModel");
App.Current.Resources.Add("theViewModel", this.DataContext);
base.OnNavigatedFrom(e);
}

When returning to the page, pull it back out again and assign it to the page.


         protected override void OnNavigatedTo(NavigationEventArgs e)  
{
if (App.Current.Resources["theViewModel"] != null)
this.DataContext = App.Current.Resources["theViewModel"];
}

Friday, May 28, 2010

Using Silverlight Isolated Storage

To store string data in Isolated Storage:

IsolatedStorageSettings.ApplicationSettings.Add("KeyName",
    Value);

To remove:

if (IsolatedStorageSettings.ApplicationSettings.Contains("KeyName"))
    IsolatedStorageSettings.ApplicationSettings.Remove("KeyName");

Sunday, May 23, 2010

DependencyProperty

Sometimes it is necessary to bind to a custom property on a Silverlight Control.  In order for that to work properly, you need to set it up as a DependencyProperty. 

public int Denominator
{
    get { return (int)this.GetValue(DenominatorProperty); }
    set { this.SetValue(DenominatorProperty, value); }
}

public static readonly DependencyProperty DenominatorProperty
    = DependencyProperty.Register(
            "Denominator", //Property Name
            typeof(int), //Property Type
            typeof(QualitativeStatusIndicator),  // type of Control
            new PropertyMetadata(
                 0, // default value
                 Denominator_PropertyChangedCallback));  // callback

private static void Denominator_PropertyChangedCallback(
    DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    QualitativeStatusIndicator myControl
       = d as QualitativeStatusIndicator;
    myControl.SetStatus();
}