public Scenario4() { this.InitializeComponent(); try { formatterShortDateLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{month.integer}/{day.integer}/{year.full} {hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); formatterLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); calendar = new Windows.Globalization.Calendar(); decimalFormatter = new Windows.Globalization.NumberFormatting.DecimalFormatter(); geofenceCollection = new GeofenceItemCollection(); eventCollection = new EventDescriptorCollection(); // Get a geolocator object geolocator = new Geolocator(); geofences = GeofenceMonitor.Current.Geofences; // using data binding to the root page collection of GeofenceItems RegisteredGeofenceListBox.DataContext = geofenceCollection; // using data binding to the root page collection of GeofenceItems associated with events GeofenceEventsListBox.DataContext = eventCollection; FillRegisteredGeofenceListBoxWithExistingGeofences(); FillEventListBoxWithExistingEvents(); accessInfo = DeviceAccessInformation.CreateFromDeviceClass(DeviceClass.Location); accessInfo.AccessChanged += OnAccessChanged; coreWindow = CoreWindow.GetForCurrentThread(); // this needs to be set before InitializeComponent sets up event registration for app visibility coreWindow.VisibilityChanged += OnVisibilityChanged; // register for state change events GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged; GeofenceMonitor.Current.StatusChanged += OnGeofenceStatusChanged; } catch (UnauthorizedAccessException) { if (DeviceAccessStatus.DeniedByUser == accessInfo.CurrentStatus) { rootPage.NotifyUser("Location has been disabled by the user. Enable access through the settings charm.", NotifyType.StatusMessage); } else if (DeviceAccessStatus.DeniedBySystem == accessInfo.CurrentStatus) { rootPage.NotifyUser("Location has been disabled by the system. The administrator of the device must enable location access through the location control panel.", NotifyType.StatusMessage); } else if (DeviceAccessStatus.Unspecified == accessInfo.CurrentStatus) { rootPage.NotifyUser("Location has been disabled by unspecified source. The administrator of the device may need to enable location access through the location control panel, then enable access through the settings charm.", NotifyType.StatusMessage); } } catch (Exception ex) { // GeofenceMonitor failed in adding a geofence // exceptions could be from out of memory, lat/long out of range, // too long a name, not a unique name, specifying an activation // time + duration that is still in the past // If ex.HResult is RPC_E_DISCONNECTED (0x80010108): // The Location Framework service event state is out of synchronization // with the Windows.Devices.Geolocation.Geofencing.GeofenceMonitor. // To recover remove all event handlers on the GeofenceMonitor or restart the application. // Once all event handlers are removed you may add back any event handlers and retry the operation. rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage); } }
public Scenario4() { this.InitializeComponent(); try { formatterShortDateLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{month.integer}/{day.integer}/{year.full} {hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); formatterLongTime = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", Windows.Globalization.CalendarIdentifiers.Gregorian, Windows.Globalization.ClockIdentifiers.TwentyFourHour); calendar = new Windows.Globalization.Calendar(); decimalFormatter = new Windows.Globalization.NumberFormatting.DecimalFormatter(); geofenceCollection = new GeofenceItemCollection(); eventCollection = new EventDescriptorCollection(); // Get a geolocator object geolocator = new Geolocator(); geofences = GeofenceMonitor.Current.Geofences; // using data binding to the root page collection of GeofenceItems RegisteredGeofenceListBox.DataContext = geofenceCollection; // using data binding to the root page collection of GeofenceItems associated with events GeofenceEventsListBox.DataContext = eventCollection; FillRegisteredGeofenceListBoxWithExistingGeofences(); FillEventListBoxWithExistingEvents(); #if WINDOWS_APP accessInfo = DeviceAccessInformation.CreateFromDeviceClass(DeviceClass.Location); accessInfo.AccessChanged += OnAccessChanged; #endif coreWindow = CoreWindow.GetForCurrentThread(); // this needs to be set before InitializeComponent sets up event registration for app visibility coreWindow.VisibilityChanged += OnVisibilityChanged; // register for state change events GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged; GeofenceMonitor.Current.StatusChanged += OnGeofenceStatusChanged; } #if WINDOWS_APP catch (UnauthorizedAccessException) { if (DeviceAccessStatus.DeniedByUser == accessInfo.CurrentStatus) { rootPage.NotifyUser("Location has been disabled by the user. Enable access through the settings charm.", NotifyType.StatusMessage); } else if (DeviceAccessStatus.DeniedBySystem == accessInfo.CurrentStatus) { rootPage.NotifyUser("Location has been disabled by the system. The administrator of the device must enable location access through the location control panel.", NotifyType.StatusMessage); } else if (DeviceAccessStatus.Unspecified == accessInfo.CurrentStatus) { rootPage.NotifyUser("Location has been disabled by unspecified source. The administrator of the device may need to enable location access through the location control panel, then enable access through the settings charm.", NotifyType.StatusMessage); } } #endif catch (Exception ex) { // GeofenceMonitor failed in adding a geofence // exceptions could be from out of memory, lat/long out of range, // too long a name, not a unique name, specifying an activation // time + duration that is still in the past // If ex.HResult is RPC_E_DISCONNECTED (0x80010108): // The Location Framework service event state is out of synchronization // with the Windows.Devices.Geolocation.Geofencing.GeofenceMonitor. // To recover remove all event handlers on the GeofenceMonitor or restart the application. // Once all event handlers are removed you may add back any event handlers and retry the operation. rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage); } }