如何在我的WP7应用程序中禁用位置服务

时间:2011-10-31 02:44:53

标签: windows-phone-7 geolocation

我正在制作一个Windows手机应用程序,显示最近的校园班车(以及其他内容)。 Windows Phone要求应用程序允许用户关闭应用程序中的位置服务。

所以,我在设置页面上为它添加了一个切换按钮,但它似乎没有做任何事情。

这是我声明geocoordinatewatcher的viewmodel。


    public MainViewModel() 
            { 
                geoWatcher = new GeoCoordinateWatcher(); 
                if (geoWatcher.TryStart(false, TimeSpan.FromSeconds(30)  )==false ) 
                { MessageBox.Show("The location services are disabled for this app. We can't detect the nearby stops. To turn location services back on, go to the settings page.", "Warning", MessageBoxButton.OK); } 
            } 

            private GeoCoordinateWatcher geoWatcher; 


            public GeoCoordinateWatcher GeoWatcher 
            { 
                get 
                { 
                    return geoWatcher; 
                } 
                set 
                { 
                    if (geoWatcher != value) 
                    { 
                        geoWatcher = value; 
                        NotifyPropertyChanged("GeoWatcher"); 
                    } 
                    if(geoWatcher.Status== GeoPositionStatus.Disabled) 
                    { 
                        geoWatcher.Stop(); 
                    } 
                } 

            } 

这是设置页面的大部分


            public SettingsPage() 
            { 
                InitializeComponent(); 

                if (App.ViewModel.GeoWatcher.Status == GeoPositionStatus.Ready) 
                { 
                    locToggle.IsChecked = true; 
                    locToggle.Content = "On"; 
                } 

                else 
                { 
                    locToggle.IsChecked = false; 
                    locToggle.Content = "Off"; 

                } 
            } 

            private void toggleChecked(object sender, RoutedEventArgs e) 
            { 
                locToggle.Content = "On"; 
                App.ViewModel.GeoWatcher.Start(); 
                MessageBox.Show("this is the status " + App.ViewModel.GeoWatcher.Status.ToString(), "Info", MessageBoxButton.OK); //for debugging 

            } 

            private void toggleUnchecked(object sender, RoutedEventArgs e) 
            { 
                locToggle.Content = "Off"; 
                App.ViewModel.GeoWatcher.Stop(); 
                MessageBox.Show("this is the status " + App.ViewModel.GeoWatcher.Status.ToString(), "Info", MessageBoxButton.OK); //for debugging 
            } 


当我关闭切换开关并单击“设置”页面时,返回到它,再次重新启用切换。

我尝试在要调试的功能上添加一个消息框,但状态始终显示为“就绪”,我的应用仍然使用位置服务,即使我将切换为“关闭”。

我是否应该在代码中添加一些内容,以便在设置页面上禁用切换时,切换将正确地使我的应用停止在我的应用中使用位置服务?或者我应该检查除GeoPositionStatus之外的其他内容?我无法弄清楚如何让我的应用程序实际更改位置服务权限或PositionStatus。

我查看了这个页面here,但由于我按照页面底部的示例进行操作,但仍然感到困惑,但无济于事。我搜索了StackOverflow,但我似乎无法找到与WP类似的问题。我也在AppHub论坛上发布了这个。

谢谢!

2 个答案:

答案 0 :(得分:1)

在MainViewModel中,您需要在使用geocoordinatewatcher之前检查他们是否允许位置服务。

if(settings.LocationAllowed)
  {all your code for using location}

答案 1 :(得分:1)

你应该考虑几个因素/点,其中大部分是你的。无论如何,你可能会发现这些有用。

  1. 您的应用程序设置切换只应在设备上启用位置服务时显示
  2. GeoPositionStatus只是Enum,其中包含状态类型。
  3. StatusChanged是要检查设备设置更改的事件。 See this.
  4. 您无法从应用程序更改设备设置。
  5. 在观察程序上调用start之前添加事件处理程序。