// When Delete button is pressed private async void OnDeletePhoneCall() { var messageDialog = new MessageDialog("Delete this PhoneCall?", "Delete confirmation"); messageDialog.Commands.Add(new UICommand("Cancel", (command) => { })); messageDialog.Commands.Add(new UICommand("Delete", async (command) => { try { LoadingData = true; CrudResult = await _phoneCallRepository.DeletePhoneCallAsync(SelectedPhoneCall.Id); _eventAggregator.GetEvent<PhoneCallDeletedEvent>().Publish(SelectedPhoneCall); } catch (HttpRequestException ex) { ErrorMessageTitle = ErrorMessagesHelper.DeletePhoneCallFailedError; 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); // Note: Each time app selects from main page (PhoneCallListPage) detail page (PhoneCallDetailPage) is recreated. // Meaning that constructor is run and SelectedPhoneCall is null. // If SuspendAndTerminate (e.g. debug mode) SelectedPhoneCall is saved to SessionState (because of [RestorableState] attribute). // Therefore, if SelectedPhoneCall has been saved, use it instead of doing GetPhoneCallAsync. if (SelectedPhoneCall == null) { string errorMessage = string.Empty; int phoneCallId = (int)navigationParameter; if (phoneCallId == 0) { SelectedPhoneCall = new PhoneCall(); SelectedPhoneCall.ValidateProperties(); RunAllCanExecute(); } else { try { LoadingData = true; CrudResult = await _phoneCallRepository.GetPhoneCallAsync(phoneCallId); SelectedPhoneCall = JsonConvert.DeserializeObject<List<PhoneCall>>(CrudResult.Content.ToString()).FirstOrDefault<PhoneCall>(); } 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 OnUpdatePhoneCall() { string errorMessage = string.Empty; bool isCreating = false; SelectedPhoneCall.ValidateProperties(); var updateErrors = SelectedPhoneCall.GetAllErrors().Values.SelectMany(pc => pc).ToList(); if (updateErrors.Count == 0) { try { LoadingData = true; if (SelectedPhoneCall.Id == 0) { isCreating = true; CrudResult = await _phoneCallRepository.CreatePhoneCallAsync(SelectedPhoneCall); SelectedPhoneCall = JsonConvert.DeserializeObject<List<PhoneCall>>(CrudResult.Content.ToString()).FirstOrDefault<PhoneCall>(); } else { CrudResult = await _phoneCallRepository.UpdatePhoneCallAsync(SelectedPhoneCall); } } catch (ModelValidationException mvex) { // there were server-side validation errors DisplayPhoneCallErrorMessages(mvex.ValidationResult); } catch (HttpRequestException ex) { ErrorMessageTitle = isCreating ? ErrorMessagesHelper.CreatePhoneCallFailedError : ErrorMessagesHelper.UpdatePhoneCallFailedError; 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(); } }