Exemplo n.º 1
0
        public IViewFor ResolveView(INavigatableViewModel vm)
        {
            try
            {
                var viewType = typeof(IViewFor <>).MakeGenericType(vm.GetType());
                var view     = _container.Resolve(viewType) as Page;

                if (!(view is IViewFor ret))
                {
                    throw new Exception(
                              $"Resolve service type '{viewType.FullName}' does not implement '{typeof(IViewFor).FullName}'.");
                }

                view.Title          = vm.Title;
                ret.ViewModel       = vm;
                view.BindingContext = vm;
                return(ret);
            }
            catch (Exception ex)
            {
                _diagnosticsFacade.TrackError(ex);
            }

            return(null);
        }
Exemplo n.º 2
0
        public Task NavigateToMainPage(INavigatableViewModel viewModel)
        {
            CurrentView = _viewFactory.ResolveView(viewModel);

            _loggingService.Info($"Navigating to MainPage: {CurrentView}");

            return(_navigationFacade.NavigateToMainPage(CurrentView));
        }
Exemplo n.º 3
0
        public Task NavigateModalAsync(INavigatableViewModel viewModel)
        {
            CurrentView = _viewFactory.ResolveView(viewModel);

            _loggingService.Info($"Opened Modal page: {CurrentView}");

            return(_navigationFacade.PushModalAsync(CurrentView));
        }
Exemplo n.º 4
0
        private NavigationEntry CreateNavigationEntry(INavigatableViewModel viewModel)
        {
            var navigationCommand = new RelayCommand(
                () =>
            {
                _vmDisplayService.DisplayAsync(viewModel.GetType());
            });

            return(new NavigationEntry(navigationCommand, viewModel.NavigationDescription));
        }
Exemplo n.º 5
0
        public IViewFor ResolveView(INavigatableViewModel vm)
        {
            var viewType = typeof(IViewFor <>).MakeGenericType(vm.GetType());
            var view     = _container.Resolve(viewType) as Page;

            if (!(view is IViewFor ret))
            {
                throw new Exception($"Resolve service type '{viewType.FullName}' does not implement '{typeof(IViewFor).FullName}'.");
            }

            view.Title    = vm.Title;
            ret.ViewModel = vm;
            return(ret);
        }
Exemplo n.º 6
0
        public IViewFor ResolveView(INavigatableViewModel vm, string name)
        {
            var viewType = typeof(IViewFor <>).MakeGenericType(vm.GetType());
            var view     = _container.ResolveNamed(name, viewType) as Page;

            if (!(view is IViewFor viewFor))
            {
                throw new Exception(
                          $"Resolve service type '{viewType.FullName}' does not implement '{typeof(IViewFor).FullName}'.");
            }

            view.Title          = vm.Title;
            viewFor.ViewModel   = vm;
            view.BindingContext = vm;
            return(viewFor);
        }
Exemplo n.º 7
0
        IObservable <Page> pageForViewModel(INavigatableViewModel vm)
        {
            if (vm == null)
            {
                return(Observable.Empty <Page>());
            }

            var ret = ViewLocator.Current.ResolveView(vm);

            if (ret == null)
            {
                var msg = $"Couldn't find a View for ViewModel. You probably need to register an IViewFor<{vm.GetType().Name}>";
                return(Observable.Throw <Page>(new Exception(msg)));
            }

            ret.ViewModel = vm;

            // ReSharper disable once SuspiciousTypeConversion.Context
            var pg = (Page)ret;

            pg.Title = vm.Title;
            return(Observable.Return(pg));
        }
Exemplo n.º 8
0
        public void DisplayPopup(FrameworkElement element, INavigatableViewModel viewModel)
        {
            if (_frame == null || element == null || viewModel == null)
                return;

            Popup popup = new Popup();

            popup.DataContext = viewModel;

            viewModel.CloseRequested += viewModel_CloseRequested;

            Grid grid = new Grid();
            grid.Name = "PopupRootLayout";
            element.Tap += element_Tap;
            grid.Tap += grid_Tap;
            grid.Width = _frame.ActualWidth;
            grid.Height = _frame.ActualHeight;
            grid.VerticalAlignment = VerticalAlignment.Stretch;
            grid.HorizontalAlignment = HorizontalAlignment.Stretch;
            grid.Background = new SolidColorBrush(Color.FromArgb(145, 0, 0, 0));
            element.VerticalAlignment = VerticalAlignment.Top;
            element.HorizontalAlignment = HorizontalAlignment.Center;
            grid.Children.Add(element);

            popup.Closed += (s, e) =>
            {
                viewModel.CloseRequested -= viewModel_CloseRequested;
                grid.Tap -= grid_Tap;
                element.Tap -= element_Tap;
                viewModel.OnClose();
                _popup = null;
            };

            popup.Child = grid;
            popup.IsOpen = true;
            _popup = popup;
        }
Exemplo n.º 9
0
 /// <summary>
 /// 创建包含页面
 /// </summary>
 /// <param name="viewModel"></param>
 /// <param name="page"></param>
 public ViewModelPage(INavigatableViewModel viewModel, INavigatablePage page)
 {
     ViewModel = viewModel;
     Page      = page;
     Key       = viewModel.Name;
 }
Exemplo n.º 10
0
 /// <summary>
 /// 创建包含页面
 /// </summary>
 /// <param name="viewModel"></param>
 public ViewModelPage(INavigatableViewModel viewModel)
 {
     ViewModel = viewModel;
     Key       = viewModel.Name;
 }
Exemplo n.º 11
0
 public void NavigateTo(INavigatableViewModel viewModel)
 {
     this.viewModelToNavigateTo = viewModel;
     this.OnNavigation(this, new NavigationEventArgs(this.viewModelToNavigateTo));
 }
Exemplo n.º 12
0
 public Task NavigateModalAsync(INavigatableViewModel viewModel)
 {
     CurrentPage = ResolveView(viewModel);
     return(_navigationFacade.PushModalAsync(CurrentPage));
 }
 public void NavigateAndClean(INavigatableViewModel viewModel)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 14
0
        IObservable<Page> pageForViewModel(INavigatableViewModel vm)
        {
            if (vm == null) return Observable.Empty<Page>();

            var ret = ViewLocator.Current.ResolveView(vm);
            if (ret == null)
            {
                var msg =$"Couldn't find a View for ViewModel. You probably need to register an IViewFor<{vm.GetType().Name}>";
                return Observable.Throw<Page>(new Exception(msg));
            }

            ret.ViewModel = vm;

            var pg = (Page)ret;
            pg.Title = vm.Title;
            return Observable.Return(pg);
        }
        /// <summary>
        /// Called when the page is navigated to. Initializes properties, such as
        /// the navigation service and view model, as well as restores context
        /// from the suspension manager.
        /// </summary>
        /// <param name="e">The event args.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (e == null)
            {
                return;
            }

            // Get NavigationService
            if (this.navigationService == null)
            {
                this.navigationService = NavigationService.GetNavigationService(this.Frame) as NavigationService;
                if (this.navigationService == null)
                {
                    throw new InvalidOperationException("Frame not registered before page navigation.");
                }

                this.contextService = this.navigationService.ContextService;
                Debug.Assert(this.contextService != null, "A navigation service's context service should never be null.");
            }

            // Set ViewModel
            if (this.ViewModel == null)
            {
                INavigatableViewModel viewModel = this.DataContext as INavigatableViewModel;
                if (viewModel == null)
                {
                    throw new InvalidOperationException("ViewModel must implement INavigatableViewModel interface.");
                }

                this.ViewModel = viewModel;
            }

            // Load page state if this page wasn't already cached by the frame
            IDictionary<string, PageState> pageStates = this.navigationService.PageStates;
            this.pageKey = "Page-" + this.Frame.BackStackDepth;

            // Get the NavigationContext
            NavigationContextBase context = this.GetContextFromParameter(e.Parameter);

            PageState pageState = null;

            if (e.NavigationMode == NavigationMode.New)
            {
                // Clear existing state for forward navigation when adding a new page to the
                // navigation stack
                var nextPageKey = this.pageKey;
                int nextPageIndex = this.Frame.BackStackDepth;
                while (pageStates.Remove(nextPageKey))
                {
                    nextPageIndex++;
                    nextPageKey = "Page-" + nextPageIndex;
                }

                // TODO: Also clean up stored navigation contexts when removing forward stack.
            }
            else
            {
                // Load page state using the same strategy for loading suspended state and
                // recreating pages discarded from cache
                pageStates.TryGetValue(this.pageKey, out pageState);
            }

            // Activate the ViewModel
            await this.ViewModel.Activate(this.navigationService, context, pageState);
        }