예제 #1
0
        private void Navigate()
        {
            var parameters = Parameters.ToDictionary(x => x.ParameterName, x => x.Value);

            var navigator = NavigationProperties.GetNavigator(AssociatedObject);

            if (navigator == null)
            {
                throw new ImpossibleNavigationRequestException("The Navigator for this UI element is not avaialable. Please ensure the current view has been loaded into a frame, or that the NavigationProperties.Navigator attached property has been set.");
            }

            var request = new RouteValueDictionary(parameters);

            PrepareRequest(request);
            navigator.ProcessRequest(new NavigationRequest(request));
        }
        /// <summary>
        /// Creates an <see cref="INavigator"/> bound to the navigation service that owns a given source
        /// element. This method can  be called multiple times for the same <paramref name="sourceElement"/>.
        /// </summary>
        /// <param name="sourceElement">A UI element that lives inside the frame that you want a navigator
        /// for.</param>
        /// <returns>
        /// An instance of the <see cref="INavigator"/> interface which can be used for navigation.
        /// </returns>
        public INavigator GetOwningNavigator(UIElement sourceElement)
        {
            Guard.ArgumentNotNull(sourceElement, "sourceElement");

            var existingNavigator = NavigationProperties.GetNavigator(sourceElement);

            if (existingNavigator != null)
            {
                return(existingNavigator);
            }

            Func <INavigationService> lazyFrameGetter = () =>
            {
                var frame = null as Frame;
                DependencyObject parent = sourceElement;
                while (parent != null)
                {
                    frame = parent as Frame;
                    if (frame != null)
                    {
                        break;
                    }
                    parent = VisualTreeHelper.GetParent(parent);
                }

                if (frame == null)
                {
                    throw new ImpossibleNavigationRequestException("The Navigator for this UI element is not avaialable. Please ensure the current view has been loaded into a frame, or that the NavigationProperties.Navigator attached property has been set.");
                }

                var wrapper = new FrameNavigationServiceWrapper(sourceElement.Dispatcher, frame);
                return(wrapper);
            };

            return(NewNavigator(lazyFrameGetter, null));
        }