Created
November 21, 2019 13:20
-
-
Save icebeam7/d5820c8fdbd0d8b2e1b5c24037594e57 to your computer and use it in GitHub Desktop.
MVVMDemo: ListProductViewModel.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.Windows.Input; | |
using System.Threading.Tasks; | |
using System.Collections.ObjectModel; | |
using MVVMDemo.Models; | |
using Xamarin.Forms; | |
namespace MVVMDemo.ViewModels | |
{ | |
public class ListProductViewModel : BaseViewModel | |
{ | |
private ObservableCollection<Product> _products; | |
public ObservableCollection<Product> Products | |
{ | |
get { return _products; } | |
set { _products = value; OnPropertyChanged(); } | |
} | |
private Product _selectedProduct; | |
public Product SelectedProduct | |
{ | |
get { return _selectedProduct; } | |
set { _selectedProduct = value; OnPropertyChanged(); } | |
} | |
public ICommand GoToDetailsCommand { private set; get; } | |
public INavigation Navigation { get; set; } | |
public ListProductViewModel(INavigation navigation) | |
{ | |
Navigation = navigation; | |
GoToDetailsCommand = new Command<Type>(async (pageType) => await GoToDetails(pageType)); | |
Products = new ObservableCollection<Product>(); | |
Products.Add(new Product() { ID = 1, Name = "Leche", Price = 10.30, IsAvailable = true }); | |
Products.Add(new Product() { ID = 2, Name = "Chocolates", Price = 12.78, IsAvailable = false }); | |
Products.Add(new Product() { ID = 3, Name = "Galletas", Price = 8, IsAvailable = true }); | |
} | |
async Task GoToDetails(Type pageType) | |
{ | |
if (SelectedProduct != null) | |
{ | |
var page = (Page)Activator.CreateInstance(pageType); | |
page.BindingContext = new ProductViewModel() | |
{ | |
IsAvailable = SelectedProduct.IsAvailable, | |
Name = SelectedProduct.Name, | |
Price = SelectedProduct.Price | |
}; | |
await Navigation.PushAsync(page); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment