为什么 CLLocationManager 不传递事件来处理?
Why doesn't CLLocationManager deliver events to handle?
如果我仅在 UIThread 上创建 CLLocationManager 实例,则会触发 LocationUpdated 事件。
为什么会这样?
Xamarin 和 Apple 文档中没有任何线索表明 CLLocationManager 在 UIThread 上必须是 created
。
有些代码要求locationManager.RequestWhenInUseAuthorization();
NSLocationWhenInUseUsageDescription 设置在 Info.plist
private void CreateLocationManagerWorkingOption () {
ExecuteOnMainThread (() => {
locationManager = new CLLocationManager ();
});
locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
};
}
private void CreateLocationManagerNotWorkingOption () {
ExecuteOnSomeThread(()=> {
locationManager = new CLLocationManager ();
});
locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
};
}
private void StartTrackingImpl() {
ExecuteOnMainThread (() => locationManager?.StartUpdatingLocation ());
}
您可以从每个具有活动运行循环的线程创建和处理它。
来自CLLocationManagerDelegate documentation:
The methods of your delegate object are called from the thread in which you started the corresponding location services. That thread must itself have an active run loop, like the one found in your application’s main thread.
我想我知道为什么了。
首先,我遇到了同样的问题。我无法让 LocationUpdates 在三种不同的设备上工作。我已经测试了很多,但 CLLocationManager 事件仍然没有被触发。我遇到了这个问题,终于弄明白了为什么我的设备上没有触发事件。
我在线程池中实例化了 CLLoationManager。线程池由 .NET 管理。因此,我在其中实例化 CLLoationManager 的线程在一段时间后完成,因此没有地方可以触发事件。
希望我的回答对您有所帮助!
如果我仅在 UIThread 上创建 CLLocationManager 实例,则会触发 LocationUpdated 事件。
为什么会这样?
Xamarin 和 Apple 文档中没有任何线索表明 CLLocationManager 在 UIThread 上必须是 created
。
有些代码要求locationManager.RequestWhenInUseAuthorization(); NSLocationWhenInUseUsageDescription 设置在 Info.plist
private void CreateLocationManagerWorkingOption () {
ExecuteOnMainThread (() => {
locationManager = new CLLocationManager ();
});
locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
};
}
private void CreateLocationManagerNotWorkingOption () {
ExecuteOnSomeThread(()=> {
locationManager = new CLLocationManager ();
});
locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
};
}
private void StartTrackingImpl() {
ExecuteOnMainThread (() => locationManager?.StartUpdatingLocation ());
}
您可以从每个具有活动运行循环的线程创建和处理它。
来自CLLocationManagerDelegate documentation:
The methods of your delegate object are called from the thread in which you started the corresponding location services. That thread must itself have an active run loop, like the one found in your application’s main thread.
我想我知道为什么了。 首先,我遇到了同样的问题。我无法让 LocationUpdates 在三种不同的设备上工作。我已经测试了很多,但 CLLocationManager 事件仍然没有被触发。我遇到了这个问题,终于弄明白了为什么我的设备上没有触发事件。 我在线程池中实例化了 CLLoationManager。线程池由 .NET 管理。因此,我在其中实例化 CLLoationManager 的线程在一段时间后完成,因此没有地方可以触发事件。 希望我的回答对您有所帮助!