Tombstoning Canvas Visibility属性

时间:2012-01-06 20:36:46

标签: c# windows-phone-7.1 windows-phone-7

我在检索画布的可见性属性时遇到了一些小问题。当我检索页面状态时,即使它在逻辑删除时折叠,画布也始终可见。我尝试了一堆if else和switch语句,但没有运气。我该如何解决这个错误?提前感谢任何想要帮助的人!

以下是代码:

    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);
    }

LoadState()和SaveState()方法位于不同的类中。这些是我从墓碑上观看的视频中得到的:

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);
    }

1 个答案:

答案 0 :(得分:1)

不保存System.Windows.Visibility,而是保存bool,表明控件是否可见。

this.SaveState(isVisibleKey,coachNotes.Visibility == Visibility.Visible); 


canvasNotes.Visibility = this.LoadState<bool>(isVisibleKey) ? Visibility.Visible : Visibility.Collapsed;