/// <summary> /// Registers commands for UI elements /// </summary> private void RegisterCommands() { // Handle add new field AddFieldCommand = new Command(async(p) => { // Show the add field dialog var res = await DialogHelper.ShowClosableDialog <AddFieldDialog>(async(d) => { try { var m = (d.DataContext as AddFieldDialogViewModel); var field = new FieldModel(new ApiField() { Name = m.FieldName, FieldType = m.SelectedFieldType.Id }); // Send the field to the API to be stored under the item var resp = await KryptPadApi.SaveFieldAsync(Category.Id, Item.Id, field.Field); field.Id = resp.Id; // Add field to the list AddFieldToCollection(field); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } }); }); // Handle item delete DeleteItemCommand = new Command(async(p) => { // Prompt user to delete the item var promptResp = await DialogHelper.Confirm( "This action cannot be undone. All data associated with this item will be deleted. Are you sure you want to delete this item?", async(c) => { try { // Delete the item await KryptPadApi.DeleteItemAsync(Category.Id, Item.Id); // Navigate back to items page NavigationHelper.Navigate(typeof(ItemsPage), null); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } }); }); // Handle copy field value CopyFieldValueCommand = new Command(async(p) => { try { var field = p as FieldModel; // Check if field value is null if (!string.IsNullOrWhiteSpace(field.Value)) { // Create a data package var package = new DataPackage(); package.SetText(field.Value); // Set the value of the field to the clipboard Clipboard.SetContent(package); } } catch (Exception) { // Failed await DialogHelper.ShowMessageDialogAsync("Failed to copy text to clipboard."); } }); // Delete field DeleteFieldCommand = new Command(DeleteFieldCommandHandler); // Rename field RenameFieldCommand = new Command(RenameFieldCommandHandler); // Generate password GeneratePasswordCommand = new Command(GeneratePasswordCommandHandler); }