Reputation: 107
I have small problem with retrieving the state of a canvas' visibility property. When I retrieve the page state, the canvas is always visible even if it was collapsed when it was tombstoned. I tried a bunch of if else and switch statements but with no luck. How do I fix this bug? Thanks in advance to anyone who wants to help!
Here's the code:
private const string coachPivotKey = "CoachPivotKey";
private const string isVisibleKey = "IsVisibleKey";
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
this.SaveState(coachPivotKey, coachPivot.SelectedIndex);
this.SaveState(isVisibleKey, canvasNotes.Visibility);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
coachPivot.SelectedItem = coachPivot.Items[this.LoadState<int>(coachPivotKey)];
canvasNotes.Visibility = this.LoadState<Visibility>(isVisibleKey);
base.OnNavigatedTo(e);
}
The LoadState() and SaveState() methods are in a different class. These I got from a video I watched on tombstoning:
public static void SaveState(this PhoneApplicationPage phoneApplicationPage, string key, object value)
{
if (phoneApplicationPage.State.ContainsKey(key))
{
phoneApplicationPage.State.Remove(key);
}
phoneApplicationPage.State.Add(key, value);
}
public static T LoadState<T>(this PhoneApplicationPage phoneApplicationPage, string key)
{
if (phoneApplicationPage.State.ContainsKey(key))
{
return (T)phoneApplicationPage.State[key];
}
return default(T);
}
Upvotes: 1
Views: 275
Reputation: 4268
Instead of saving a System.Windows.Visibility
, save a bool
indicating whether the control is visible.
this.SaveState(isVisibleKey,coachNotes.Visibility == Visibility.Visible);
canvasNotes.Visibility = this.LoadState<bool>(isVisibleKey) ? Visibility.Visible : Visibility.Collapsed;
Upvotes: 1