Windows phone 7 如何在我的WP7应用程序中禁用位置服务
我正在制作一个windows phone应用程序,其中显示最近的校园班车的位置(以及其他内容)。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(); } } }Windows phone 7 如何在我的WP7应用程序中禁用位置服务,windows-phone-7,geolocation,Windows Phone 7,Geolocation,我正在制作一个windows phone应用程序,其中显示最近的校园班车的位置(以及其他内容)。Windows Phone要求应用程序允许用户关闭应用程序中的位置服务 所以,我在一个设置页面上为它添加了一个切换,但它似乎没有任何作用 这是我声明geocoordinatewatcher的viewmodel public MainViewModel() { geoWatcher = new GeoCoordinateWatcher();
if(settings.LocationAllowed)
{all your code for using location}
公共主视图模型()
{
geoWatcher=新的GeoCoordinateWatcher();
if(geoWatcher.TryStart(false,TimeSpan.FromSeconds(30))==false)
{MessageBox.Show(“此应用禁用了位置服务。我们无法检测到附近的站点。若要重新打开位置服务,请转到设置页面。”,“警告”,MessageBoxButton.OK);}
}
私人地球协调员、地球观测员;
公共地理坐标追踪者地理观察者
{
得到
{
返回地球观察者;
}
设置
{
if(geoWatcher!=值)
{
地理观察者=价值;
NotifyPropertyChanged(“地理观察者”);
}
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
}
公共设置页面()
{
初始化组件();
if(App.ViewModel.geovatcher.Status==GeoPositionStatus.Ready)
{
locToggle.IsChecked=true;
locToggle.Content=“On”;
}
其他的
{
locToggle.IsChecked=false;
locToggle.Content=“关”;
}
}
私有无效切换已选中(对象发送器、路由目标)
{
locToggle.Content=“On”;
App.ViewModel.GeoWatcher.Start();
MessageBox.Show(“这是状态”+App.ViewModel.geovatcher.status.ToString(),“Info”,MessageBoxButton.OK);//用于调试
}
private void toggleUnchecked(对象发送方、路由目标方)
{
locToggle.Content=“关”;
App.ViewModel.GeoWatcher.Stop();
MessageBox.Show(“这是状态”+App.ViewModel.geovatcher.status.ToString(),“Info”,MessageBoxButton.OK);//用于调试
}
当我关闭切换并单击“设置”页面以外的位置,然后返回该页面时,切换将再次启用
我试着在要调试的功能上添加一个消息框,但状态总是显示“就绪”,并且我的应用程序仍然使用位置服务,即使我将开关切换到“关闭”
我是否应该向代码中添加一些内容,以便在设置页面上禁用位置服务时,切换可以正确地使我的应用停止在我的应用中使用位置服务?或者我应该检查地理位置以外的其他东西?我想不出一种方法来让我的应用程序实际更改位置服务权限或位置状态
我看了这一页,但仍然感到困惑,因为我遵循了页面底部的示例,但没有任何效果。我搜索了StackOverflow,但似乎找不到与WP类似的问题。不过我也在AppHub论坛上发布了这篇文章
谢谢 在您的MainViewModel中,您需要在使用geocoordinatewatcher之前检查他们是否允许定位服务 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(); } } }
if(settings.LocationAllowed)
{all your code for using location}
你可能应该考虑一些因素/要点,其中大部分是你已经考虑过的。无论如何,你可能会觉得这些很有用
GeoPositionStatus
只是一个包含状态类型的Enum
StatusChanged
是要处理的事件,用于检查设备设置的更改李>
start
之前添加事件处理程序