Esempio n. 1
0
        public async override void OnNavigatedTo(NavigatedToEventArgs e, Dictionary <string, object> viewModelState)
        {
            base.OnNavigatedTo(e, viewModelState);
            var    queryText    = e.Parameter as String;
            string errorMessage = string.Empty;

            this.SearchTerm = queryText;
            this.QueryText  = '\u201c' + queryText + '\u201d';

            try
            {
                Collection <Product> products;
                if (queryText == PreviousSearchTerm)
                {
                    products = PreviousResults;
                }
                else
                {
                    var searchResults = await _productCatalogRepository.GetFilteredProductsAsync(queryText, 0);

                    products        = searchResults.Products;
                    TotalCount      = searchResults.TotalCount;
                    PreviousResults = products;
                }

                var productViewModels = new List <ProductViewModel>();
                foreach (var product in products)
                {
                    productViewModels.Add(new ProductViewModel(product));
                }

                // Communicate results through the view model
                this.Results   = new ReadOnlyCollection <ProductViewModel>(productViewModels);
                this.NoResults = !this.Results.Any();

                // Update VM status
                PreviousSearchTerm = SearchTerm;
            }
            catch (Exception ex)
            {
                errorMessage = string.Format(CultureInfo.CurrentCulture, _resourceLoader.GetString("GeneralServiceErrorMessage"), Environment.NewLine, ex.Message);
            }

            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                await _alertMessageService.ShowAsync(errorMessage, _resourceLoader.GetString("ErrorServiceUnreachable"));
            }
        }