如果复选框检查页面之间的条件

时间:2011-10-13 19:58:24

标签: c# silverlight windows-phone-7

我目前在我的settings.xaml页面下有一个复选框,用户点击该复选框以允许位置服务。

private void cbLocationAllow_Checked(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Location Services is now enabled");
}

private void cbLocationAllow_Unchecked(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Location Services is now disabled");
}

我在我的主页上有位置感知的bing地图,我希望在找到位置之前检查settings.xaml上的复选框的状态。

我认为如果是其他情况会是一个条件,但我不太确定如何实现这个,因为复选框在另一页上。

我建议使用以下代码,但我收到错误

Settings.SetSetting("allowLocation",true);

在设置页面上:

cbLocationAllowChecked(...)
{
Settings.SetSetting("allowLocation",true);
}

cbLocationAllowUnchecked(...)
{
Settings.SetSetting("allowLocation,false);
}

在主页上作为条件

MapButtonClicked(...)
{
if (!Settings.HasSetting("allowLocation") || !((bool)Settings.GetSetting("allowLocation"))
        MessageBox.Show("Allow app to use your location?, "Location Services",MessageBoxButtons.OkCancel);
//handle result
else{

StartLocationSearch();
}

}

任何可以帮助我的建议或链接都​​会很棒。

谢谢,

1 个答案:

答案 0 :(得分:2)

使用IsolatedStorageSettings课程保存您的设置。

存储设置

private void cbLocationAllow_Checked(object sender, RoutedEventArgs e)
{
  var settings = IsolatedStorageSettings.ApplicationSettings;

  settings["allowLocation"] = true;
  settings.Save();
}

阅读设置

var settings = IsolatedStorageSettings.ApplicationSettings;
bool allowLocation = false;

if( settings.Contains( "allowLocation" ) ) {
  allowLocation = (bool)settings["allowLocation"];
}