コード例 #1
0
        public CategoryViewModel(Category category, INavigationService navigationService)
        {
            _category = category;
            _navigationService = navigationService;
            CategoryNavigationCommand = new DelegateCommand(NavigateToCategory);

            if (category != null && category.Products != null)
            {
                int position = 0;
                Products = new ReadOnlyCollection<ProductViewModel>(category.Products
                                                                            .Select(p => new ProductViewModel(p) { ItemPosition = position++ })
                                                                            .ToList());
            }
        }
        public void OnNavigatedTo_Fill_Items_And_Title()
        {
            var repository = new MockProductCatalogRepository();

            repository.GetCategoryAsyncDelegate = (categoryId) =>
            {
                Category category = null;

                if (categoryId == 1)
                {
                    category = new Category { Id = categoryId,
                        Title = "CategoryTitle"
                    };
                }

                return Task.FromResult(category);
            };
            repository.GetProductsAsyncDelegate = i =>
            {
                ICollection<Product> products = null;
                if (i == 1)
                {
                    products = new Collection<Product>(new List<Product>
                                    {
                                        new Product(),
                                        new Product(),
                                        new Product()
                                    });
                }
                return Task.FromResult(products);
            };

           var viewModel = new GroupDetailPageViewModel(repository, null, null);
            viewModel.OnNavigatedTo(new NavigatedToEventArgs { Parameter = 1, NavigationMode = NavigationMode.New }, null);

            Assert.IsNotNull(viewModel.Items);
            Assert.AreEqual(3, ((ICollection<ProductViewModel>)viewModel.Items).Count);
            Assert.AreEqual("CategoryTitle", viewModel.Title);
        }