示例#1
0
        public async Task Creating_ViewModelBeingInitializable_InitializesViewModel_WithPassedParameters()
        {
            // Arrange
            var viewModel = new InitializibleMockViewModel();

            var initParam1 = "Hello";
            var initParam2 = 12345;

            _serviceLocatorMock.Setup(f => f.GetService(It.IsAny <Type>())).Returns(viewModel);

            // Act
            var actualViewModel = await _sut.CreateAsync <InitializibleMockViewModel>(initParam1, initParam2);

            // Assert
            Assert.IsNotNull(actualViewModel);
            Assert.AreEqual(viewModel, actualViewModel);
            Assert.IsTrue(actualViewModel.WasInitialized);
            Assert.IsNotNull(actualViewModel.InitParams);
            Assert.AreEqual(initParam1, actualViewModel.InitParams[0]);
            Assert.AreEqual(initParam2, actualViewModel.InitParams[1]);
        }
        public async Task <Page> NavigateToAsync <T>(CancellationToken cancellationToken, Action <T> preInitAction) where T : ViewModel
        {
            Type vmType = typeof(T);
            Type pageType;

            if (!RegistryByViewModel.TryGetValue(vmType, out pageType))
            {
                throw new InvalidOperationException();
            }

            Page page = (Page)await PageFactory.CreateAsync(pageType);

            T viewModel = await ViewModelFactory.CreateAsync <T>();

            preInitAction?.Invoke(viewModel);

            page.BindingContext = viewModel;

            await this.NavigationPage.PushAsync(page);

            await viewModel.InitializeAsync(CancellationToken.None);

            return(page);
        }