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 New button is pressed private void OnNewPhoneCall() { SelectedPhoneCall = new PhoneCall(); SelectedPhoneCall.ValidateProperties(); RunAllCanExecute(); }