从PhoneApplicationService中保存的首选项中选中初始化单选按钮

时间:2011-10-16 12:31:44

标签: c# windows-phone-7 radio-button

开发Windows手机应用程序。我有一个首选项页面,它始终加载默认值。我使用PhoneApplicationService来保存已检查的单选按钮的Name属性,以保留下次使用的值。

在首选项页面的构造函数类中,我应该如何使用PhoneApplicationService

中保存的单选按钮的名称进行编码
<RadioButton Content="Black" Name="ic_black" GroupName="gColor" Click="icChange" />
<RadioButton Content="Blue" GroupName="gColor" Name="ic_blue" Click="icChange" />
<RadioButton Content="Brown" GroupName="gColor" Name="ic_brown" Click="icChange" />
<RadioButton Content="Gray" GroupName="gColor" Name="ic_gray"  Click="icChange" />

以上代码是首选项的xaml。 icChange方法获取单选按钮的名称并将其保存在PhoneApplicationService.State["color"]

{
            InitializeComponent();
            object prefs;
            string temp;
            if (prefs.State.TryGetValue("color", out prefs))
            {
                temp =  prefs.ToString();
                //the name of the radio button to be checked is the variable temp..
                //code to initialize the correct radio button from prefs
            }
}

1 个答案:

答案 0 :(得分:0)

我会提出类似下面的内容,即使它不是最好的:

temp =  prefs.ToString();  
if(ic_black.Name == temp) ic_black.IsChecked = true;
else if(ic_blue.Name == temp) ic_blue.IsChecked = true;
else if(ic_brown.Name == temp) ic_brown.IsChecked = true;
else if(ic_gray.Name == temp) ic_gray.IsChecked = true;
else //Do the default case