Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created November 21, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/9b431c8c3a1f0ce5023c17cc4cc28b95 to your computer and use it in GitHub Desktop.
Save icebeam7/9b431c8c3a1f0ce5023c17cc4cc28b95 to your computer and use it in GitHub Desktop.
MVVMDemo: ProductViewModel.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MVVMDemo.ViewModels
{
public class ProductViewModel : BaseViewModel
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; OnPropertyChanged(); }
}
private double _price;
public double Price
{
get { return _price; }
set { _price = value; OnPropertyChanged(); }
}
private bool _isAvailable;
public bool IsAvailable
{
get { return _isAvailable; }
set { _isAvailable = value; OnPropertyChanged(); }
}
public ICommand ClearCommand { private set; get; }
public ICommand SendEmailCommand { private set; get; }
public ProductViewModel()
{
ClearCommand = new Command(() => Clear());
SendEmailCommand = new Command(async () => await SendEmail());
}
void Clear()
{
Name = string.Empty;
Price = 0;
IsAvailable = false;
}
async Task SendEmail()
{
try
{
var message = new EmailMessage
{
Subject = "New Product!",
Body = $"Product Name: {Name}\nPrice: {Price:N2}\nAvailability? {(IsAvailable ? "Yes" : "No")}",
To = new List<string> { "luis@luisbeltran.mx" }
};
await Email.ComposeAsync(message);
}
catch (Exception)
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment