Windows应用商店应用C#XAML:NavigationHelper LoadState / SaveState单选按钮

时间:2015-08-03 02:55:04

标签: c# xaml windows-store-apps windows-8.1

如何在前后导航时保存单选按钮选择?

对于文本框,我知道它是这样的:

private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        if (e.PageState != null)
        {
            this.text5_input.Text = e.PageState["txtContents"] as string;

        }
    }


 private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
    {
        e.PageState["txtContents"] = this.text5_input.Text;
    }

然而,当我们使用类似的单选代码按钮时,会弹出一个错误,指出无法将字符串(e.PageState)转换为bool(单选按钮)。

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以使用RadioButtonInstance.IsChecked.ToString并恢复数据使用bool.Parse((bool)e.PageState [" rbState"])

private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    if (e.PageState != null)
    {
        this.text5_input.Text = e.PageState["txtContents"] as string;
        this.RadioButtonInstance.IsChecked = (bool)e.PageState["rbState"];
    }
}

private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
    e.PageState["txtContents"] = this.text5_input.Text;
    e.PageState["rbState"] = this.RadioButtonInstance.IsChecked;
}

理论上这是为了布尔,你能试试吗?