public async void OnGeofenceStateChanged(GeofenceMonitor sender, object e) { var reports = sender.ReadReports(); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { foreach (GeofenceStateChangeReport report in reports) { GeofenceState state = report.NewState; Geofence geofence = report.Geofence; string eventDescription = GetTimeStampedMessage(geofence.Id); eventDescription += " (" + state.ToString(); if (state == GeofenceState.Removed) { eventDescription += "/" + report.RemovalReason.ToString() + ")"; AddEventDescription(eventDescription); // remove the geofence from the client side geofences collection Remove(geofence); // empty the registered geofence listbox and repopulate geofenceCollection.Clear(); FillRegisteredGeofenceListBoxWithExistingGeofences(); } else if (state == GeofenceState.Entered || state == GeofenceState.Exited) { // NOTE: You might want to write your app to take particular // action based on whether the app has internet connectivity. eventDescription += ")"; AddEventDescription(eventDescription); } } }); }
//设置geofence listbox的数据源 private void FillRegisteredGeofenceListBoxWithhExistingGeofences() { //获取当前所有Geofence IList <Geofence> geofences = GeofenceManager.Instance.GetGeofences(); //清空并重新填充geoItemCollection geoItemCollection.Clear(); foreach (var g in geofences) { geoItemCollection.Insert(0, new GeofenceItem(g)); } //重新绑定 RegisteredGeofenceListBox.DataContext = geoItemCollection; if (geoItemCollection.Count > 0) { noItemHintTextBlock.Visibility = Visibility.Collapsed; } else { noItemHintTextBlock.Visibility = Visibility.Visible; } }