// When Delete button is pressed private async void OnDeleteState() { var messageDialog = new MessageDialog("Delete this State?", "Delete confirmation"); messageDialog.Commands.Add(new UICommand("Cancel", (command) => { })); messageDialog.Commands.Add(new UICommand("Delete", async (command) => { try { LoadingData = true; CrudResult = await _stateRepository.DeleteStateAsync(SelectedState.Id); _eventAggregator.GetEvent<StateDeletedEvent>().Publish(SelectedState); } catch (HttpRequestException ex) { ErrorMessageTitle = ErrorMessagesHelper.DeleteAsyncFailedError; ErrorMessage = ex.Message; } finally { LoadingData = false; RunAllCanExecute(); _navService.GoBack(); } })); messageDialog.DefaultCommandIndex = 0; await messageDialog.ShowAsync(); if (ErrorMessage != null && ErrorMessage != string.Empty) { messageDialog = new MessageDialog(ErrorMessage, ErrorMessageTitle); await messageDialog.ShowAsync(); _navService.GoBack(); } }
public async override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState) { base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState); if (viewModelState != null) { if (navigationMode == NavigationMode.Refresh) { var stateErrorsCollection = RetrieveEntityStateValue<IDictionary<string, ReadOnlyCollection<string>>>("stateErrorsCollection", viewModelState); if (stateErrorsCollection != null) { SelectedState.SetAllErrors(stateErrorsCollection); } } } // Note: Each time app selects from main page (StateListPage) detail page (StateDetailPage) is recreated. // Meaning that constructor is run and SelectedState is null. // If SuspendAndTerminate (e.g. debug mode) SelectedState is saved to SessionState (because of [RestorableState] attribute). // Therefore, if SelectedState has been saved, use it instead of doing GetStateAsync. if (SelectedState == null) { string errorMessage = string.Empty; int stateId = (int)navigationParameter; if (stateId == 0) { SelectedState = new State(); SelectedState.ValidateProperties(); RunAllCanExecute(); } else { try { LoadingData = true; CrudResult = await _stateRepository.GetStateAsync(stateId); SelectedState = JsonConvert.DeserializeObject<List<State>>(CrudResult.Content.ToString()).FirstOrDefault<State>(); } catch (HttpRequestException ex) { ErrorMessageTitle = ErrorMessagesHelper.HttpRequestExceptionError; //TODO: Log stack trace to database here ErrorMessage = string.Format("{0}", ex.Message); } finally { LoadingData = false; } if (ErrorMessage != null && ErrorMessage != string.Empty) { MessageDialog messageDialog = new MessageDialog(ErrorMessage, ErrorMessageTitle); await messageDialog.ShowAsync(); _navService.GoBack(); } } } RunAllCanExecute(); }
// When Update button is pressed private async void OnUpdateState() { string errorMessage = string.Empty; bool isCreating = false; SelectedState.ValidateProperties(); var updateErrors = SelectedState.GetAllErrors().Values.SelectMany(pc => pc).ToList(); if (updateErrors.Count == 0) { try { LoadingData = true; if (SelectedState.Id == 0) { isCreating = true; CrudResult = await _stateRepository.CreateStateAsync(SelectedState); SelectedState = JsonConvert.DeserializeObject<List<State>>(CrudResult.Content.ToString()).FirstOrDefault<State>(); } else { CrudResult = await _stateRepository.UpdateStateAsync(SelectedState); } } catch (ModelValidationException mvex) { // there were server-side validation errors DisplayStateErrorMessages(mvex.ValidationResult); } catch (HttpRequestException ex) { ErrorMessageTitle = isCreating ? ErrorMessagesHelper.CreateAsyncFailedError : ErrorMessagesHelper.UpdateAsyncFailedError; ErrorMessage = ex.Message; } finally { LoadingData = false; RunAllCanExecute(); } if (ErrorMessage != null && ErrorMessage != string.Empty) { MessageDialog messageDialog = new MessageDialog(ErrorMessage, ErrorMessageTitle); await messageDialog.ShowAsync(); _navService.GoBack(); } } else { RunAllCanExecute(); } }