我正在使用Windows 10 SDK(内部版本10586)开发Windows移动应用程序,我需要订阅Geolocator的StatusChanged事件。
问题是,当我使用“设置”>“位置”菜单关闭地理位置时,未调用我的OnStatusChanged方法。
public async Task StartTracking()
{
// Request permission to access location
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
_geolocator = new Geolocator
{
ReportInterval = 2000,
MovementThreshold = 10d,
DesiredAccuracyInMeters = 10,
DesiredAccuracy = PositionAccuracy.High
};
_geolocator.PositionChanged += OnPositionChanged;
_geolocator.StatusChanged += OnStatusChanged;
break;
case GeolocationAccessStatus.Denied:
Messenger.Default.Send(new GeolocationDeniedAccessMessage(true));
break;
case GeolocationAccessStatus.Unspecified:
Messenger.Default.Send(new GeolocationDeniedAccessMessage(true));
break;
}
}
private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)
{
switch (e.Status)
{
case PositionStatus.Ready:
Messenger.Default.Send(new GeolocationAvailabilityMessage(true));
Messenger.Default.Send(new GeolocationDeniedAccessMessage(false));
break;
case PositionStatus.Initializing:
break;
case PositionStatus.NoData:
Messenger.Default.Send(new GeolocationAvailabilityMessage(false));
break;
case PositionStatus.Disabled:
Messenger.Default.Send(new GeolocationDeniedAccessMessage(true));
break;
case PositionStatus.NotInitialized:
break;
case PositionStatus.NotAvailable:
Messenger.Default.Send(new GeolocationAvailabilityMessage(false));
break;
}
}
最佳答案
更新
问题似乎是EventHandler
的注册。如果一旦调用StartTracking()
禁用了位置,则GeolocationAccessStatus
将被“拒绝”,因此一旦用户在设置中打开位置跟踪功能,就不会注册EventHandler
,也不会调用OnStatusChanged
。
尝试将Geolocator
的初始化移出GeolocationAccessStatus.Allowed
案例
确保已在清单Location
的功能选项卡中选择了package.appxmanifest
。