c# - 禁用对位置服务 API 的访问

标签 c# windows-phone-7

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

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

Your app must provide in-app settings that allow the user to enable and disable your app's access to and use of location from the Location Service API.

任何人都可以提供有关我如何执行此操作的进一步帮助吗?

最佳答案

将此代码粘贴到 MainPage.xaml 中的 InitializeComponent(); 之后。您必须通过此行using System.IO.IsolatedStorage;添加对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();
}

还创建一个带有 ToggleSwitchSettings.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.");
}

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

关于c# - 禁用对位置服务 API 的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429635/

相关文章:

c# - 使 VirtualizingStackPanel 不清理已加载的项目

c# - 从存储过程中获取 XML

c# - 绑定(bind)到队列 <string>。 UI永不更新

c# - 密码加密

c# - 如何在 WP7 上的 SQL 数据库中存储图像

silverlight - Windows Phone 7 silverlight 用户控制 : data binding not working on custom property

c# - 在单元测试中使用最小起订量模拟经过身份验证的用户

c# - 如何在 C# 中使用 PDFLib 9 生成新的 PDF?

c# - 如何让普通的 WebRequest 异步且可等待?

windows-phone-7 - Windows Phone 7 崩溃