示例#1
0
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            Debug.WriteLine("LoadState");
            // If a hardware Back button is present, hide the "soft" Back button
            // in the command bar, and register a handler for presses of the hardware
            if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                HardwareButtons.BackPressed += HardwareButtons_BackPressed;
                Debug.WriteLine("HWButtonTypeIsPresent");
            }

            //var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            GeolocationAccessStatus status = await Geolocator.RequestAccessAsync();
            if (status == GeolocationAccessStatus.Allowed)
            {
                settingsLink.Visibility = Visibility.Collapsed;
                bool goToInitState = false;
                //if a saved state is found, read the values and update the state
                if (e.PageState != null)
                {
                    if (e.PageState.ContainsKey("avg_start_position_lat") && (double)e.PageState["avg_start_position_lat"] != 0
                        && e.PageState.ContainsKey("avg_start_position_lon") && (double)e.PageState["avg_start_position_lon"] != 0
                        && e.PageState.ContainsKey("avg_start_position_alt") && e.PageState.ContainsKey("avg_start_position_accuracy")
                        && e.PageState.ContainsKey("result_m") && e.PageState.ContainsKey("result_ft"))
                    {
                        // check if start position is too old
                        if (e.PageState.ContainsKey("start_position_timestamp"))
                        {
                            DateTime oldTime = new DateTime();
                            bool parseOK = DateTime.TryParse((string)e.PageState["start_position_timestamp"], out oldTime);

                            // if parse succesful, set timestamp, if not, set it to now
                            if (parseOK)
                            {
                                start_position_timestamp = oldTime;
                            }
                            else
                            {
                                start_position_timestamp = DateTime.Now;
                            }

                            long difference = DateTime.Now.Ticks - oldTime.Ticks;
                            TimeSpan differenceSpan = new TimeSpan(difference);
                            Debug.WriteLine("difference: " + differenceSpan.TotalHours + "h " + differenceSpan.TotalMinutes + "min " + differenceSpan.TotalSeconds + "sec");

                            if (differenceSpan.TotalHours < MAX_START_POSITION_AGE_IN_HOURS)
                            {
                                Debug.WriteLine("start_position not too old");

                                avg_start_position.Latitude = (double)e.PageState["avg_start_position_lat"];
                                avg_start_position.Longitude = (double)e.PageState["avg_start_position_lon"];
                                avg_start_position.Altitude = (double)e.PageState["avg_start_position_alt"];
                                avg_start_point_accuracy = (double)e.PageState["avg_start_position_accuracy"];
                                result_m = (double)e.PageState["result_m"];
                                result_ft = (double)e.PageState["result_ft"];


                                Debug.WriteLine("page state restored");
                                Debug.WriteLine("LoadState - result_m: " + result_m + " result_ft: " + result_ft);
                                progressBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
                                statusLbl.Text = RESUMING;
                                setStartBtn.IsEnabled = false;
                                setGeolocatorToReportState();
                            }
                            else
                            {
                                // start position was too old -> 
                                goToInitState = true;
                            }
                        }
                    }
                    else
                    {
                        goToInitState = true;
                        // avg start position is zero
                        //Debug.WriteLine("start position was zero");
                    }
                }
                // no saved state, launching first time
                else
                {
                    goToInitState = true;
                    Debug.WriteLine("no saved state, to init state");

                }
                if (goToInitState)
                {
                    setGeolocatorToInitState(STARTING_GPS, true, true);
                }
                setLabels();

            }
            else
            {
                Debug.WriteLine("no location consent");
    
                // The app is not allowed to location
                setStartBtn.IsEnabled = false;
                statusLbl.Text = LOCATION_OPTED_OUT;
                settingsLink.Visibility = Visibility.Visible;
                //var msg = new MessageDialog(LOCATION_CONSENT_QUESTION);
                //var okBtn = new UICommand("yes", new UICommandInvokedHandler(CommandHandler));
                //var cancelBtn = new UICommand("no", new UICommandInvokedHandler(CommandHandler));
                //msg.Commands.Add(okBtn);
                //msg.Commands.Add(cancelBtn);
                //IUICommand result = await msg.ShowAsync();
            }
        }
示例#2
0
 /// <summary>
 /// Populates the page with content passed during navigation.  Any saved state is also
 /// provided when recreating a page from a prior session.
 /// </summary>
 /// <param name="sender">
 /// The source of the event; typically <see cref="NavigationHelper"/>
 /// </param>
 /// <param name="e">Event data that provides both the navigation parameter passed to
 /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
 /// a dictionary of state preserved by this page during an earlier
 /// session.  The state will be null the first time a page is visited.</param>
 private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
 {
 }