コード例 #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);
        }
コード例 #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);
        }
コード例 #3
0
        public async Task RestoreApplicationStateAsync()
        {
            if (!await this.statePersistor.ContainsStateAsync())
            {
                return;
            }

            var states = await this.statePersistor.GetAllStatesAsync();

            var lastState = states.Last();

            var allViewTypes = await Task.WhenAll(states.Select(s => this.viewLocator.GetViewTypeAsync(s.ViewModelType)));

            // this should also cause navigating to the last View, so no need to navigate there manually
            this.platformNavigator.RestoreNavigationState(this.statePersistor.NavigationPath, allViewTypes);

            foreach (var state in states.Where(s => s != null))
            {
                var viewType = await this.viewLocator.GetViewTypeAsync(state.ViewModelType);

                var attribute = viewType.GetTypeInfo().GetCustomAttribute <AssociatedViewModelAttribute>();
                var viewModel = await this.viewModelLocator.GetInstanceAsync(attribute.ViewModel);

                var activationData = state.ActivationData;
                var vmState        = state.ViewModelState.Value;

                // try to activate the ViewModel
                if (activationData == null)
                {
                    // TODO: when VM has StatePersistence set to None || StateOnly, this always gets called because activationData is set to null
                    ViewModelActivator.ActivateViewModel(viewModel, NavigationType.Forward);
                }
                else
                {
                    ViewModelActivator.ActivateViewModel(viewModel, NavigationType.Forward, activationData);
                }

                this.statePersistor.SetViewModelState(viewModel, vmState);
            }
        }