Beispiel #1
0
        public static bool IsTargetRelativePop(ShellNavigationParameters request)
        {
            if (request?.TargetState?.Location?.OriginalString == null)
            {
                return(false);
            }

            bool isRelativePopping = false;

            // If the user is popping with PopAsync or ".."
            // we need to know this so we don't clear the query parameters off
            // the destination location

            var dest = request.TargetState?.Location?.OriginalString;

            foreach (var path in RetrievePaths(dest))
            {
                if (path != "..")
                {
                    isRelativePopping = false;
                    break;
                }
                else
                {
                    isRelativePopping = true;
                }
            }

            return(isRelativePopping);
        }
Beispiel #2
0
        internal static NavigationRequest GetNavigationRequest(Shell shell, Uri uri, bool enableRelativeShellRoutes = false, bool throwNavigationErrorAsException = true, ShellNavigationParameters shellNavigationParameters = null)
        {
            uri = FormatUri(uri, shell);

            // figure out the intent of the Uri
            NavigationRequest.WhatToDoWithTheStack whatDoIDo = CalculateStackRequest(uri);

            Uri request = ConvertToStandardFormat(shell, uri);


            var possibleRouteMatches = GenerateRoutePaths(shell, request, uri, enableRelativeShellRoutes);

            if (possibleRouteMatches.Count == 0)
            {
                if (throwNavigationErrorAsException)
                {
                    throw new ArgumentException($"unable to figure out route for: {uri}", nameof(uri));
                }

                return(null);
            }
            else if (possibleRouteMatches.Count > 1)
            {
                string[] matches = new string[possibleRouteMatches.Count];
                int      i       = 0;
                foreach (var match in possibleRouteMatches)
                {
                    matches[i] = match.PathFull;
                    i++;
                }

                string matchesFound = String.Join(",", matches);

                if (throwNavigationErrorAsException)
                {
                    throw new ArgumentException($"Ambiguous routes matched for: {uri} matches found: {matchesFound}", nameof(uri));
                }

                return(null);
            }

            var theWinningRoute = possibleRouteMatches[0];

            RequestDefinition definition =
                new RequestDefinition(theWinningRoute, shell);

            NavigationRequest navigationRequest = new NavigationRequest(definition, whatDoIDo, request.Query, request.Fragment);

            return(navigationRequest);
        }
Beispiel #3
0
        public async Task GoToAsync(ShellNavigationParameters shellNavigationParameters)
        {
            if (shellNavigationParameters.PagePushing != null)
            {
                Routing.RegisterImplicitPageRoute(shellNavigationParameters.PagePushing);
            }

            var  state   = shellNavigationParameters.TargetState ?? new ShellNavigationState(Routing.GetRoute(shellNavigationParameters.PagePushing), false);
            bool?animate = shellNavigationParameters.Animated;
            bool enableRelativeShellRoutes        = shellNavigationParameters.EnableRelativeShellRoutes;
            ShellNavigatingEventArgs deferredArgs = shellNavigationParameters.DeferredArgs;

            var  navigationRequest = ShellUriHandler.GetNavigationRequest(_shell, state.FullLocation, enableRelativeShellRoutes, shellNavigationParameters: shellNavigationParameters);
            bool isRelativePopping = ShellUriHandler.IsTargetRelativePop(shellNavigationParameters);

            ShellNavigationSource source = CalculateNavigationSource(_shell, _shell.CurrentState, navigationRequest);

            // If the deferredArgs are non null that means we are processing a delayed navigation
            // so the user has indicated they want to go forward with navigation
            // This scenario only comes up from UI iniated navigation (i.e. switching tabs)
            if (deferredArgs == null)
            {
                var navigatingArgs = ProposeNavigation(source, state, _shell.CurrentState != null, animate ?? true);

                if (navigatingArgs != null)
                {
                    bool accept = !navigatingArgs.NavigationDelayedOrCancelled;
                    if (navigatingArgs.DeferredTask != null)
                    {
                        accept = await navigatingArgs.DeferredTask;
                    }

                    if (!accept)
                    {
                        return;
                    }
                }
            }

            Routing.RegisterImplicitPageRoutes(_shell);

            _accumulateNavigatedEvents = true;

            var uri         = navigationRequest.Request.FullUri;
            var queryString = navigationRequest.Query;
            var queryData   = ParseQueryString(queryString);

            ApplyQueryAttributes(_shell, queryData, false, false);

            var shellItem           = navigationRequest.Request.Item;
            var shellSection        = navigationRequest.Request.Section;
            var currentShellSection = _shell.CurrentItem?.CurrentItem;
            var nextActiveSection   = shellSection ?? shellItem?.CurrentItem;

            ShellContent shellContent       = navigationRequest.Request.Content;
            bool         modalStackPreBuilt = false;

            // If we're replacing the whole stack and there are global routes then build the navigation stack before setting the shell section visible
            if (navigationRequest.Request.GlobalRoutes.Count > 0 &&
                nextActiveSection != null &&
                navigationRequest.StackRequest == NavigationRequest.WhatToDoWithTheStack.ReplaceIt)
            {
                modalStackPreBuilt = true;

                bool?isAnimated = (nextActiveSection != currentShellSection) ? false : animate;
                await nextActiveSection.GoToAsync(navigationRequest, queryData, isAnimated, isRelativePopping);
            }

            if (shellItem != null)
            {
                ApplyQueryAttributes(shellItem, queryData, navigationRequest.Request.Section == null, false);
                bool navigatedToNewShellElement = false;

                if (shellSection != null && shellContent != null)
                {
                    ApplyQueryAttributes(shellContent, queryData, navigationRequest.Request.GlobalRoutes.Count == 0, isRelativePopping);
                    if (shellSection.CurrentItem != shellContent)
                    {
                        shellSection.SetValueFromRenderer(ShellSection.CurrentItemProperty, shellContent);
                        navigatedToNewShellElement = true;
                    }
                }

                if (shellSection != null)
                {
                    ApplyQueryAttributes(shellSection, queryData, navigationRequest.Request.Content == null, false);
                    if (shellItem.CurrentItem != shellSection)
                    {
                        shellItem.SetValueFromRenderer(ShellItem.CurrentItemProperty, shellSection);
                        navigatedToNewShellElement = true;
                    }
                }

                if (_shell.CurrentItem != shellItem)
                {
                    _shell.SetValueFromRenderer(Shell.CurrentItemProperty, shellItem);
                    navigatedToNewShellElement = true;
                }

                if (!modalStackPreBuilt && currentShellSection?.Navigation.ModalStack.Count > 0)
                {
                    // - navigating to new shell element so just pop everything
                    // - or route contains no global route requests
                    if (navigatedToNewShellElement || navigationRequest.Request.GlobalRoutes.Count == 0)
                    {
                        // remove all non visible pages first so the transition just smoothly goes from
                        // currently visible modal to base page
                        if (navigationRequest.Request.GlobalRoutes.Count == 0)
                        {
                            for (int i = currentShellSection.Stack.Count - 1; i >= 1; i--)
                            {
                                currentShellSection.Navigation.RemovePage(currentShellSection.Stack[i]);
                            }
                        }

                        await currentShellSection.PopModalStackToPage(null, animate);
                    }
                }

                if (navigationRequest.Request.GlobalRoutes.Count > 0 && navigationRequest.StackRequest != NavigationRequest.WhatToDoWithTheStack.ReplaceIt)
                {
                    // TODO get rid of this hack and fix so if there's a stack the current page doesn't display
                    await Device.InvokeOnMainThreadAsync(() =>
                    {
                        return(_shell.CurrentItem.CurrentItem.GoToAsync(navigationRequest, queryData, animate, isRelativePopping));
                    });
                }
                else if (navigationRequest.Request.GlobalRoutes.Count == 0 &&
                         navigationRequest.StackRequest == NavigationRequest.WhatToDoWithTheStack.ReplaceIt &&
                         currentShellSection?.Navigation?.NavigationStack?.Count > 1)
                {
                    // TODO get rid of this hack and fix so if there's a stack the current page doesn't display
                    await Device.InvokeOnMainThreadAsync(() =>
                    {
                        return(_shell.CurrentItem.CurrentItem.GoToAsync(navigationRequest, queryData, animate, isRelativePopping));
                    });
                }
            }
            else
            {
                await _shell.CurrentItem.CurrentItem.GoToAsync(navigationRequest, queryData, animate, isRelativePopping);
            }

            _accumulateNavigatedEvents = false;

            // this can be null in the event that no navigation actually took place!
            if (_accumulatedEvent != null)
            {
                HandleNavigated(_accumulatedEvent);
            }
        }
Beispiel #4
0
        internal static NavigationRequest GetNavigationRequest(Shell shell, Uri uri, bool enableRelativeShellRoutes = false, bool throwNavigationErrorAsException = true, ShellNavigationParameters shellNavigationParameters = null)
        {
            uri = FormatUri(uri, shell);

            // figure out the intent of the Uri
            NavigationRequest.WhatToDoWithTheStack whatDoIDo = NavigationRequest.WhatToDoWithTheStack.PushToIt;
            if (uri.IsAbsoluteUri)
            {
                whatDoIDo = NavigationRequest.WhatToDoWithTheStack.ReplaceIt;
            }
            else if (uri.OriginalString.StartsWith("//", StringComparison.Ordinal) || uri.OriginalString.StartsWith("\\\\", StringComparison.Ordinal))
            {
                whatDoIDo = NavigationRequest.WhatToDoWithTheStack.ReplaceIt;
            }
            else
            {
                whatDoIDo = NavigationRequest.WhatToDoWithTheStack.PushToIt;
            }

            Uri request = ConvertToStandardFormat(shell, uri);


            var possibleRouteMatches = GenerateRoutePaths(shell, request, uri, enableRelativeShellRoutes);


            if (possibleRouteMatches.Count == 0)
            {
                if (throwNavigationErrorAsException)
                {
                    throw new ArgumentException($"unable to figure out route for: {uri}", nameof(uri));
                }

                return(null);
            }
            else if (possibleRouteMatches.Count > 1)
            {
                string[] matches = new string[possibleRouteMatches.Count];
                int      i       = 0;
                foreach (var match in possibleRouteMatches)
                {
                    matches[i] = match.PathFull;
                    i++;
                }

                string matchesFound = String.Join(",", matches);

                if (throwNavigationErrorAsException)
                {
                    throw new ArgumentException($"Ambiguous routes matched for: {uri} matches found: {matchesFound}", nameof(uri));
                }

                return(null);
            }

            var theWinningRoute          = possibleRouteMatches[0];
            RequestDefinition definition =
                new RequestDefinition(
                    ConvertToStandardFormat(shell, CreateUri(theWinningRoute.PathFull)),
                    theWinningRoute.Item,
                    theWinningRoute.Section,
                    theWinningRoute.Content,
                    theWinningRoute.GlobalRouteMatches);

            NavigationRequest navigationRequest = new NavigationRequest(definition, whatDoIDo, request.Query, request.Fragment);

            return(navigationRequest);
        }