Created
November 5, 2020 06:27
-
-
Save azimuthdeveloper/f24e0291623de080821e629710f153d5 to your computer and use it in GitHub Desktop.
viewmodelsample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Windows.Input; | |
using testxamarin.Models; | |
using Xamarin.Forms; | |
namespace testxamarin.ViewModels | |
{ | |
public class NewItemViewModel : BaseViewModel | |
{ | |
private string text; | |
private string description; | |
public NewItemViewModel() | |
{ | |
SaveCommand = new Command(OnSave, ValidateSave); | |
CancelCommand = new Command(OnCancel); | |
this.PropertyChanged += | |
(_, __) => SaveCommand.ChangeCanExecute(); | |
} | |
private bool ValidateSave() | |
{ | |
return !String.IsNullOrWhiteSpace(text) | |
&& !String.IsNullOrWhiteSpace(description); | |
} | |
public string Text | |
{ | |
get => text; | |
set => SetProperty(ref text, value); | |
} | |
public string Description | |
{ | |
get => description; | |
set => SetProperty(ref description, value); | |
} | |
public Command SaveCommand { get; } | |
public Command CancelCommand { get; } | |
private async void OnCancel() | |
{ | |
// This will pop the current page off the navigation stack | |
await Shell.Current.GoToAsync(".."); | |
} | |
private async void OnSave() | |
{ | |
Item newItem = new Item() | |
{ | |
Id = Guid.NewGuid().ToString(), | |
Text = Text, | |
Description = Description | |
}; | |
await DataStore.AddItemAsync(newItem); | |
// This will pop the current page off the navigation stack | |
await Shell.Current.GoToAsync(".."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment