/// <summary> /// Initializes the view and its data context. /// </summary> private void InitializePageState() { // Retrieve data from temporary storage if present; // otherwise, copy data from CarDataStore.Car. if (State.ContainsKey(CAR_INFO_KEY)) { _car = (Car)State[CAR_INFO_KEY]; // Restore the read-only state of the odometer text box. OdometerTextBox.IsReadOnly = (bool)State[ODOMETER_READONLY_STATE]; // Restore the change state except when the PhotoTask_Completed // method has already set the change state. if (!_hasUnsavedChanges) { _hasUnsavedChanges = (bool)State[HAS_UNSAVED_CHANGES_KEY]; } // Delete temporary storage to avoid unnecessary storage costs. State.Clear(); } else { _car = CarDataStore.Car; // Delete the temporary photo if it exists. This prevents an old // temporary photo selection from reappearing after tombstoning. CarDataStore.DeleteTempCarPhoto(); // Disable the odometer text box when displaying a saved value. OdometerTextBox.IsReadOnly = _car.InitialOdometerReading > 0; // Disable the delete car button for new car if (_car.InitialOdometerReading.Equals(0)) { var deleteButton = (ApplicationBarIconButton)this.ApplicationBar.Buttons[1]; deleteButton.IsEnabled = false; } } // Set the page data context to the car. DataContext = _car; }
/// <summary> /// Displays a warning dialog box if the user presses the back button /// and there are unsaved changes. /// </summary> /// <param name="e"></param> protected override void OnBackKeyPress( System.ComponentModel.CancelEventArgs e) { base.OnBackKeyPress(e); // If there are no changes, do nothing. if (!_hasUnsavedChanges) { return; } var result = MessageBox.Show("You are about to discard your " + "changes. Continue?", "Warning", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { CarDataStore.DeleteTempCarPhoto(); } else { e.Cancel = true; } }