コード例 #1
0
        // When Delete button is pressed
        private async void OnDeleteEntity() {
            var messageDialog = new MessageDialog("Delete this Entity?", "Delete confirmation");
            messageDialog.Commands.Add(new UICommand("Cancel", (command) =>
            {
            }));

            messageDialog.Commands.Add(new UICommand("Delete", async (command) =>
            {
                try {
                    LoadingData = true;
                    CrudResult = await _entityRepository.DeleteEntityAsync(SelectedEntity.Id);
                    _eventAggregator.GetEvent<EntityDeletedEvent>().Publish(SelectedEntity);
                }
                catch (HttpRequestException ex) {
                    ErrorMessageTitle = ErrorMessagesHelper.DeleteEntityFailedError;
                    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();
            }
        }
コード例 #2
0
        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 (EntityListPage) detail page (EntityDetailPage) is recreated.
            // Meaning that constructor is run and SelectedEntity is null.
            // If SuspendAndTerminate (e.g. debug mode) SelectedEntity is saved to SessionState (because of [RestorableState] attribute).
            // Therefore, if SelectedEntity has been saved, use it instead of doing GetEntityAsync.
            if (SelectedEntity == null) {
                string errorMessage = string.Empty;
                int entityId = (int)navigationParameter;

                if (entityId == 0) {
                    SelectedEntity = new Entity();
                    SelectedEntity.ValidateProperties();
                    RunAllCanExecute();
                }
                else {
                    try {
                        LoadingData = true;
                        CrudResult = await _entityRepository.GetEntityAsync(entityId);
                        SelectedEntity = JsonConvert.DeserializeObject<List<Entity>>(CrudResult.Content.ToString()).FirstOrDefault<Entity>();
                    }
                    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();
        }
コード例 #3
0
        // When Update button is pressed
        private async void OnUpdateEntity() {
            string errorMessage = string.Empty;
            bool isCreating = false;

            SelectedEntity.ValidateProperties();
            var updateErrors = SelectedEntity.GetAllErrors().Values.SelectMany(pc => pc).ToList();

            if (updateErrors.Count == 0) {
                try {
                    LoadingData = true;
                    if (SelectedEntity.Id == 0) {
                        isCreating = true;
                        CrudResult = await _entityRepository.CreateEntityAsync(SelectedEntity);
                        SelectedEntity = JsonConvert.DeserializeObject<List<Entity>>(CrudResult.Content.ToString()).FirstOrDefault<Entity>();
                    }
                    else {
                        CrudResult = await _entityRepository.UpdateEntityAsync(SelectedEntity);
                    }
                }
                catch (ModelValidationException mvex) {
                    // there were server-side validation errors
                    DisplayEntityErrorMessages(mvex.ValidationResult);
                }
                catch (HttpRequestException ex) {
                    ErrorMessageTitle = isCreating ? ErrorMessagesHelper.CreateEntityFailedError : ErrorMessagesHelper.UpdateEntityFailedError;
                    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();
            }
        }