我通过在调用 awaitable 之前设置一个 boolignoreNextViewportChanges
并在异步操作完成后将其重置来解决了这个问题。true
TrySetViewAsync
false
在事件处理程序中,我立即打破常规然后ignoreNextViewportChanges
仍然是真的。
所以最后,它看起来像:
bool ignoreNextViewportChanges;
public void HandleMapCenterChanged() {
Map.CenterChanged += (sender, args) => {
if(ignoreNextViewportChanges)
return;
//if you came here, the user has changed the location
//store this information somewhere and skip SetCenter next time
}
}
public async void SetCenter(BasicGeoposition center) {
ignoreNextViewportChanges = true;
await Map.TrySetViewAsync(new Geopoint(Center));
ignoreNextViewportChanges = false;
}
如果您遇到 SetCenter 可能被并行调用两次(因此最后一次调用SetCenter
尚未完成,而是SetCenter
再次调用)的情况,您可能需要使用计数器:
int viewportChangesInProgressCounter;
public void HandleMapCenterChanged() {
Map.CenterChanged += (sender, args) => {
if(viewportChangesInProgressCounter > 0)
return;
//if you came here, the user has changed the location
//store this information somewhere and skip SetCenter next time
}
}
public async void SetCenter(BasicGeoposition center) {
viewportChangesInProgressCounter++;
await Map.TrySetViewAsync(new Geopoint(Center));
viewportChangesInProgressCounter--;
}