protected virtual void OnNavigating(NavigatingCancelEventArgsBase args)
        {
            if (args.Context.Contains(IgnoreNavigatingConstant))
            {
                return;
            }
            var context = CreateContextNavigateFrom(args);

            if (context == null)
            {
                return;
            }

            args.Cancel = true;
            var navigateTask = !args.IsCancelable
                ? Empty.TrueTask
                : NavigationDispatcher.OnNavigatingAsync(context);

            navigateTask.TryExecuteSynchronously(task =>
            {
                if (!task.IsCanceled && task.IsFaulted)
                {
                    TryCompleteNavigationTask(args.Context, false);
                    NavigationDispatcher.OnNavigationFailed(context, task.Exception);
                    return;
                }

                if (task.IsCanceled || !task.Result)
                {
                    TryCompleteNavigationTask(args.Context, false);
                    if (!context.NavigationMode.IsClose())
                    {
                        NavigationDispatcher.OnNavigationCanceled(context);
                    }
                    return;
                }
                ThreadManager.Invoke(ExecutionMode.AsynchronousOnUiThread, this, context, args, (@this, ctx, e) =>
                {
                    e.Context.AddOrUpdate(IgnoreNavigatingConstant, null);
                    @this.Renavigate(ctx, e);
                });
            });
        }
Пример #2
0
        private Task <bool> TryCloseRootAsync(IViewModel viewModel, IDataContext context)
        {
            if (viewModel == null || !viewModel.Settings.State.Contains(IsRootConstant) || viewModel.Settings.Metadata.Contains(IsClosed))
            {
                return(null);
            }

            var currentView = viewModel.GetCurrentView <object>();

            var backButtonAction = XamarinFormsToolkitExtensions.SendBackButtonPressed?.Invoke(currentView);

            if (backButtonAction == null)
            {
                return(null);
            }

            var navigationContext = new NavigationContext(NavigationType.Page, NavigationMode.Back, viewModel, null, this, context);
            var task = NavigationDispatcher.OnNavigatingAsync(navigationContext);

            if (task.IsCompleted)
            {
                if (task.Result)
                {
                    viewModel.Settings.Metadata.AddOrUpdate(IsClosed, null);
                    _hasRootPage = false;
                    NavigationDispatcher.OnNavigated(navigationContext);
                }
                return(task);
            }
            return(task.TryExecuteSynchronously(t =>
            {
                if (!t.Result)
                {
                    return false;
                }
                _hasRootPage = false;
                viewModel.Settings.Metadata.AddOrUpdate(IsClosed, null);
                backButtonAction();
                NavigationDispatcher.OnNavigated(navigationContext);
                return true;
            }));
        }
Пример #3
0
        private Task <bool> CloseViewModel(INavigationDispatcher navigationDispatcher, IViewModel viewModel, IDataContext ctx)
        {
            if (!(viewModel is TViewModel))
            {
                return(Empty.FalseTask);
            }
            var removeCtx = new NavigationContext(NavigationType.Tab, NavigationMode.Remove, viewModel, null, this, ctx);

            return(NavigationDispatcher
                   .OnNavigatingAsync(removeCtx)
                   .TryExecuteSynchronously(task =>
            {
                if (task.Result)
                {
                    _lastRemoveContext = removeCtx;
                    return ItemsSource.Remove((TViewModel)viewModel);
                }
                return false;
            }));
        }