Example #1
0
        private async Task <bool> NavigateAndActivateAsync(Type viewModelType, object activationData)
        {
            var parameters = new DeactivationParameters(viewModelType, activationData);

            if (!await this.DeactivatePreviousViewModelsAsync(NavigationType.Forward, viewModelType, parameters))
            {
                return(false);
            }

            var viewType = await this.viewLocator.GetViewTypeAsync(viewModelType);

            var viewModel = await this.viewModelLocator.GetInstanceAsync(viewModelType);

            var currentViewModel = this.ActiveViewModelType;
            var currentView      = currentViewModel != null ? await this.viewLocator.GetViewTypeAsync(currentViewModel) : null;

            // activating the ViewModel instance
            if (parameters.DeactivationData == null)
            {
                ViewModelActivator.ActivatingViewModel(viewModel, NavigationType.Forward);
            }
            else
            {
                ViewModelActivator.ActivatingViewModel(viewModel, NavigationType.Forward, parameters.DeactivationData);
            }

            // navigate to target View
            this.platformNavigator.Navigate(viewType);

            // push state
            await this.statePersistor.PushStateAsync(viewModel, parameters.DeactivationData, this.platformNavigator.GetNavigationState());

            this.ActiveViewModelType = viewModelType;

            // raise navigated event
            this.Navigated?.Invoke(this, new NavigationEventArgs(NavigationType.Forward, currentViewModel, currentView, viewModelType, viewType, parameters.DeactivationData));

            // activate the ViewModel instance
            if (parameters.DeactivationData == null)
            {
                ViewModelActivator.ActivateViewModel(viewModel, NavigationType.Forward);
            }
            else
            {
                ViewModelActivator.ActivateViewModel(viewModel, NavigationType.Forward, parameters.DeactivationData);
            }

            return(true);
        }
Example #2
0
        private async Task <bool> GoBackAndActivateAsync(object deactivationData)
        {
            var nextState  = (await this.statePersistor.GetAllStatesAsync()).Select(s => s).Reverse().Skip(1).First();
            var parameters = new DeactivationParameters(nextState.ViewModelType, deactivationData);

            if (!await this.DeactivatePreviousViewModelsAsync(NavigationType.Backward, null, parameters))
            {
                return(false);
            }

            var viewModel = await this.viewModelLocator.GetInstanceAsync(nextState.ViewModelType);

            var viewType = await this.viewLocator.GetViewTypeAsync(nextState.ViewModelType);

            var lastState = await this.statePersistor.PopStateAsync();

            var lastViewType = await this.viewLocator.GetViewTypeAsync(lastState.ViewModelType);

            // activating the ViewModelInstance
            if (parameters.DeactivationData == null)
            {
                ViewModelActivator.ActivatingViewModel(viewModel, NavigationType.Backward);
            }
            else
            {
                ViewModelActivator.ActivatingViewModel(viewModel, NavigationType.Backward, parameters.DeactivationData);
            }

            // go back to previous View
            this.platformNavigator.GoBack(lastState.ViewModelType, lastViewType);

            this.ActiveViewModelType = nextState.ViewModelType;

            // raise navigated event
            this.Navigated?.Invoke(this, new NavigationEventArgs(NavigationType.Backward, lastState.ViewModelType, lastViewType, nextState.ViewModelType, viewType, parameters.DeactivationData));

            // activate the ViewModel instance
            if (parameters.DeactivationData == null)
            {
                ViewModelActivator.ActivateViewModel(viewModel, NavigationType.Backward);
            }
            else
            {
                ViewModelActivator.ActivateViewModel(viewModel, NavigationType.Backward, parameters.DeactivationData);
            }

            return(true);
        }