Now we move on to the last view-model in our application. Add a new file called StockItemDetailsPageViewModel
to our ViewModels folder in the Stocklist.Portable
project.
Let's start by implementing the private
properties:
#region Private Properties private readonly IStocklistWebServiceController _stocklistWebServiceController; private int _id; private string _name; private string _category; private decimal _price; private bool _inProgress; #endregion
You should be able to add the public
properties yourself. Here is the first to get you started:
public int Id { get { return _id; } set { if (value.Equals(_id)) { return; } _id = value; OnPropertyChanged("Id"); } }
Now we need to add the LoadAsync
function, which will use StocklistWebServiceController
to pull the data from our API for a specific StockItem
. Notice the use of the InProgress
property, this is used to track the loading progress; as we are downloading in the background we want to display this progress to the user interface via an ActivityIndicator
:
#region Methods protected override async Task LoadAsync(IDictionary<string, object> parameters) { InProgress = true; if (parameters.ContainsKey("id")) { Id = (int)parameters["id"]; } var contract = await _stocklistWebServiceController.GetStockItem(Id); if (contract != null) { this.Name = contract.Name; this.Category = contract.Category; this.Price = contract.Price; } InProgress = false; } #endregion
Then we add our constructor which will pull out the registered IoC objects and assign our private
properties accordingly:
#region Constructors public StockItemDetailsPageViewModel(INavigationService navigation, IStocklistWebServiceController stocklistWebServiceController, Func<Action, ICommand> commandFactory) : base(navigation) { _stocklistWebServiceController = stocklistWebServiceController; } #endregion
Finally, we need to register the view-model in the CommonModule
:
builer.RegisterType<StockItemDetailsPageViewModel>().InstancePerDependency();
Add the extra enum for the StockItemDetailsPage
to PageEnums.cs
:
public enum PageNames { MainPage, StocklistPage, StockItemDetailsPage }
And add the extra switch case to NavigationService
:
case PageNames.StockItemDetailsPage: return IoC.Resolve<Func<StockItemDetailsPage>>()();
3.144.16.254