2

如何禁用对位置服务 API 的访问?

我确实收到了来自 Microsoft 开发中心的一封信,其中包含以下提示:

您的应用必须提供应用内设置,以允许用户启用和禁用您的应用通过位置服务 API 访问和使用位置。

任何人都可以就我如何做这件事提供进一步的帮助吗?

4

2 回答 2

3

InitializeComponent();在 MainPage.xaml 中粘贴此代码。您必须通过这一行添加对 IsolatedStorage 的引用using System.IO.IsolatedStorage;

if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
    return;
}
else
{
    MessageBoxResult result = MessageBox.Show("Allow this app to access your location?", "Location", MessageBoxButton.OKCancel);

    if (result == MessageBoxResult.OK)
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
    }
    else
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
    }
    IsolatedStorageSettings.ApplicationSettings.Save();
}

还要创建一个带有ToggleSwitch的Settings.xaml页面,该页面具有以下代码:

if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
    if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
    {
       locationSwitch.IsChecked = true;
    }
    else
    {
       locationSwitch.IsChecked = false;
    }
}
else
{
    MessageBoxResult result = MessageBox.Show("Allow this app to access your location?", "Location", MessageBoxButton.OKCancel);

    if (result == MessageBoxResult.OK)
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
    }
    else
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
    }
    IsolatedStorageSettings.ApplicationSettings.Save();
}

private void locationSwitch_Checked(object sender, RoutedEventArgs e)
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
       IsolatedStorageSettings.ApplicationSettings.Save();
    }
}

private void locationSwitch_Unchecked(object sender, RoutedEventArgs e)
{
   if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
   {
      IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
      IsolatedStorageSettings.ApplicationSettings.Save();
   }
}

在您使用位置/GPS 数据的页面上包含以下代码:

if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
{
     //Do Something
}
else
{
     MessageBox.Show("Please enable location services to use this feature. You can turn it on from Settings.");
}

这肯定会有所帮助。我用的一样。如果这对您也有帮助,请投票并标记为答案:)

于 2013-05-09T18:54:42.247 回答
0

您的应用程序是否使用位置服务,并且您需要能够禁用它,或者您是否一般要求?

如果它是第一个,那么只需停止收集数据并在您的应用程序中禁用它。如果是第二个,则进入 WPmanifest 并取消选中它

于 2013-05-07T22:30:46.830 回答