コード例 #1
0
 public static async Task DeactivateViewModelAsync(object target, NavigationType navigationType, DeactivationParameters parameters)
 {
     var deactivate = target as IDeactivate;
     if (deactivate != null)
     {
         await deactivate.DeactivateAsync(navigationType, parameters);
     }
 }
コード例 #2
0
        public static async Task<bool> CanDeactivateViewModelAsync(object target, NavigationType navigationType, DeactivationParameters parameters)
        {
            var query = target as IDeactivateQuery;
            if (query != null)
            {
                if (!await query.CanDeactivateAsync(navigationType, parameters))
                {
                    return false;
                }
            }

            return true;
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
0
        private async Task<bool> DeactivatePreviousViewModelsAsync(NavigationType navigationType, Type newViewModelTypeOverride, DeactivationParameters parameters, bool rootNavigation)
        {
            IEnumerable<State> stack = await this.statePersistor.GetAllStatesAsync();
            bool navigationTypeOverriden = false;

            if (stack.Any())
            {
                var viewModelsToDeactivate = stack
                    .SkipWhile(i => !rootNavigation && i.ViewModelType != newViewModelTypeOverride)
                    .Select(i => i.ViewModelType)
                    .Reverse()
                    .ToList();

                if (viewModelsToDeactivate.Count == 0)
                {
                    viewModelsToDeactivate.Add(stack.Last().ViewModelType);
                }
                else
                {
                    // target ViewModel already exists on the stack, meaning from the perspective of the existing ViewModels, the navigation is going back
                    navigationType = NavigationType.Backward;
                    navigationTypeOverriden = true;
                }

                // first check if all can deactivate
                foreach (var viewModelType in viewModelsToDeactivate)
                {
                    var viewModel = await this.viewModelLocator.GetInstanceAsync(viewModelType);
                    if (!await ViewModelActivator.CanDeactivateViewModelAsync(viewModel, navigationType, parameters))
                    {
                        return false;
                    }
                }

                // if all can deactivate, do so
                foreach (var viewModelType in viewModelsToDeactivate)
                {
                    var viewModel = await this.viewModelLocator.GetInstanceAsync(viewModelType);
                    await ViewModelActivator.DeactivateViewModelAsync(viewModel, navigationType, parameters);
                }

                if (navigationTypeOverriden)
                {
                    foreach (var viewModelType in viewModelsToDeactivate)
                    {
                        // pop the extra ViewModels from the persistence stack
                        await this.statePersistor.PopStateAsync();

                        // when navigating forward to existing ViewModel (large screen with the first View visible) we must manually unhook existing ViewTypes, since they are no longer active
                        var viewType = await this.viewLocator.GetViewTypeAsync(viewModelType);
                        //this.platformNavigator.UnhookType(viewType);
                        this.platformNavigator.GoBack(viewModelType, viewType);
                    }
                }
            }

            return true;
        }
コード例 #6
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, false))
            {
                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;
        }
コード例 #7
0
        private async Task<bool> NavigateAndActivateAsync(Type viewModelType, object activationData, bool isRoot)
        {
            var parameters = new DeactivationParameters(viewModelType, activationData);

            if (!await this.DeactivatePreviousViewModelsAsync(NavigationType.Forward, viewModelType, parameters, isRoot))
            {
                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;
        }
コード例 #8
0
        private async Task <bool> DeactivatePreviousViewModelsAsync(NavigationType navigationType, Type newViewModelTypeOverride, DeactivationParameters parameters)
        {
            IEnumerable <State> stack = await this.statePersistor.GetAllStatesAsync();

            bool navigationTypeOverriden = false;

            if (stack.Any())
            {
                var viewModelsToDeactivate = stack
                                             .SkipWhile(i => i.ViewModelType != newViewModelTypeOverride)
                                             .Select(i => i.ViewModelType)
                                             .Reverse()
                                             .ToList();

                if (viewModelsToDeactivate.Count == 0)
                {
                    viewModelsToDeactivate.Add(stack.Last().ViewModelType);
                }
                else
                {
                    // target ViewModel already exists on the stack, meaning from the perspective of the existing ViewModels, the navigation is going back
                    navigationType          = NavigationType.Backward;
                    navigationTypeOverriden = true;
                }

                // first check if all can deactivate
                foreach (var viewModelType in viewModelsToDeactivate)
                {
                    var viewModel = await this.viewModelLocator.GetInstanceAsync(viewModelType);

                    if (!await ViewModelActivator.CanDeactivateViewModelAsync(viewModel, navigationType, parameters))
                    {
                        return(false);
                    }
                }

                // if all can deactivate, do so
                foreach (var viewModelType in viewModelsToDeactivate)
                {
                    var viewModel = await this.viewModelLocator.GetInstanceAsync(viewModelType);

                    await ViewModelActivator.DeactivateViewModelAsync(viewModel, navigationType, parameters);
                }

                if (navigationTypeOverriden)
                {
                    foreach (var viewModelType in viewModelsToDeactivate)
                    {
                        // pop the extra ViewModels from the persistence stack
                        await this.statePersistor.PopStateAsync();

                        // when navigating forward to existing ViewModel (large screen with the first View visible) we must manually unhook existing ViewTypes, since they are no longer active
                        var viewType = await this.viewLocator.GetViewTypeAsync(viewModelType);

                        //this.platformNavigator.UnhookType(viewType);
                        this.platformNavigator.GoBack(viewModelType, viewType);
                    }
                }
            }

            return(true);
        }
コード例 #9
0
 public async Task DeactivateAsync(NavigationType navigationType, DeactivationParameters parameters)
 {
     parameters.OverrideDefaultResult(navigationType, false);
 }