我正在使用
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;
}
}
最佳答案 UPDATE
问题似乎是EventHandler的注册.如果在调用StartTracking()时禁用位置,则GeolocationAccessStatus将被“拒绝”,因此一旦用户在设置中打开位置跟踪,EventHandler将永远不会被注册并且OnStatusChanged将不会被调用.
尝试将Geolocator的初始化移出GeolocationAccessStatus.Allowed案例
确保您已在清单package.appxmanifest的功能选项卡中选择了位置.