Windows Phone保留页面状态不起作用

时间:2012-06-29 04:55:44

标签: c# windows-phone

当我转到另一个页面时,我正在尝试保留页面状态,但出于某种原因,当我返回到此页面时,这些项目将返回到其默认状态。为什么不起作用?它似乎完全遵循教程......

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);


        State["Title"] = TitleTextBox.Text;

        //all of those are RadioButtons - if their is a better way to do it then please comment :)
        int i = new int();
        if (RB0.IsChecked.Value)
            i = 0;
        else if (RB1.IsChecked.Value)
            i = 1;
        else if (RB2.IsChecked.Value)
            i = 2;
        else if (RB3.IsChecked.Value)
            i = 3;
        State["CheckedRB"] = i;

    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (State.ContainsKey("Title"))
            TitleTextBox.Text = State["Title"] as string;
        if (State.ContainsKey("CheckedRB"))
        {
            int i = (int)State["CheckedRB"];

            if (i == 0)
                RB0.IsChecked = true;
            else if (i == 1)
                RB1.IsChecked = true;
            else if (i == 2)
                RB2.IsChecked = true;
            else if (i == 3)
                RB3.IsChecked = true;
        }
     }

编辑:我通过添加断点来跟踪问题。

            protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);


        State["Title"] = TitleTextBox.Text;
        string look0 = WorkOutName.Text; 
        string look = State["WorkOutName"] as string; 

        int i = new int();
        if (RB0.IsChecked.Value)
            i = 0;
        else if (RB1.IsChecked.Value)
            i = 1;
        else if (RB2.IsChecked.Value)
            i = 2;
        else if (RB3.IsChecked.Value)
            i = 3;
        State["CheckedRB"] = i;   <-------- breakpoint

以下是结果:

(OnNavigatedFrom)
look0 : "Text From TextBox"
look1 : "Text From TextBox"
i : (0, 1, 2, or 3)

与OnNavigatedTo相同:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (State.ContainsKey("Title"))
            TitleTextBox.Text = State["Title"] as string; <--breakpoint
        if (State.ContainsKey("CheckedRB"))
        {
            int i = (int)State["CheckedRB"]; <--breakpoint

            if (i == 0)
                RB0.IsChecked = true;
            else if (i == 1)
                RB1.IsChecked = true;
            else if (i == 2)
                RB2.IsChecked = true;
            else if (i == 3)
                RB3.IsChecked = true;
        }
     }

并且其中任何一个(在OnNavigatedTo上)都没有。

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码,似乎工作正常。我不知道这是不是你的情况,但请记住,如果你在应用程序中有以下导航流程:

Page1 (forward navigation)-> Page2 (back navigation)-> Page1 (forward navigation)-> Page2

如果您在第一次访问时保存了Page2中的状态,则在再次返回Page2时将不会保留该状态,这是因为在进行后向导航时,页面将被丢弃,该页面的状态字典也将被丢弃。您可以在documentation

中详细了解相关内容