Exemplo n.º 1
0
        public virtual void UpdateRootPage(NavigationPage page)
        {
            if (_rootPage != null)
            {
                _rootPage.Pushed       -= OnPushed;
                _rootPage.Popped       -= OnPopped;
                _rootPage.PoppedToRoot -= OnPopped;
            }
            if (page != null)
            {
                page.Pushed       += OnPushed;
                page.Popped       += OnPopped;
                page.PoppedToRoot += OnPopped;
            }
            _rootPage = page;

            var currentPage = CurrentContent as Page ?? page;

            if (currentPage != null)
            {
                var context = currentPage.DataContext();
                if (context != null)
                {
                    currentPage.SetNavigationParameter(NavigationProvider.GenerateNavigationParameter(context.GetType(), string.Empty));
                }
                _threadManager.Invoke(ExecutionMode.AsynchronousOnUiThread, this, currentPage,
                                      (service, p) => service.RaiseNavigated(p, p.GetNavigationParameter() as string, NavigationMode.New),
                                      OperationPriority.Low);
            }
        }
Exemplo n.º 2
0
        protected virtual void InitializeRootPage(IViewModel viewModel, IDataContext context)
        {
            var mainPage       = (Page)ViewManager.GetOrCreateView(viewModel, true, context);
            var navigationPage = mainPage as NavigationPage;

            if (WrapToNavigationPage)
            {
                navigationPage = CreateNavigationPage(mainPage);
            }

            var isRoot = navigationPage == null || ReferenceEquals(mainPage, navigationPage);

            if (navigationPage != null)
            {
                INavigationService navigationService;
                if (!ServiceProvider.TryGet(out navigationService))
                {
                    navigationService = CreateNavigationService();
                    ServiceProvider.IocContainer.BindToConstant(navigationService);
                }

                //Activating navigation provider if need
                INavigationProvider provider;
                ServiceProvider.TryGet(out provider);

                navigationService.UpdateRootPage(navigationPage, viewModel);
                mainPage = navigationPage;
            }
            Application.Current.MainPage = mainPage;

            EventHandler <Page, CancelEventArgs> handler = OnBackButtonPressed;

            XamarinFormsToolkitExtensions.BackButtonPressed -= handler;

            var mode = NavigationMode.New;

            if (isRoot)
            {
                XamarinFormsToolkitExtensions.BackButtonPressed += handler;

                if (viewModel.Settings.State.Contains(IsRootConstant))
                {
                    mode = NavigationMode.Refresh;
                }
                else
                {
                    viewModel.Settings.State.AddOrUpdate(IsRootConstant, null);
                }
                viewModel.Settings.Metadata.AddOrUpdate(ViewModelConstants.CanCloseHandler, CanCloseRootViewModel);
                NavigationDispatcher.OnNavigated(new NavigationContext(NavigationType.Page, mode, null, viewModel, this, context));
            }
            else
            {
                mainPage.SetNavigationParameter(NavigationProvider.GenerateNavigationParameter(viewModel));
                ServiceProvider.Get <INavigationProvider>().Restore(context);
            }
        }
Exemplo n.º 3
0
        protected virtual void InitializeRootPage(IViewModel viewModel, IDataContext context)
        {
            var mainPage = (Page)ServiceProvider.ViewManager.GetOrCreateView(viewModel, true, context);

            mainPage.SetNavigationParameter(NavigationProvider.GenerateNavigationParameter(viewModel));
            NavigationPage navigationPage = mainPage as NavigationPage;

            if (WrapToNavigationPage)
            {
                navigationPage = CreateNavigationPage(mainPage);
            }

            bool isRoot = ReferenceEquals(mainPage, navigationPage);

            if (navigationPage != null)
            {
                INavigationService navigationService;
                if (!IocContainer.TryGet(out navigationService))
                {
                    navigationService = CreateNavigationService();
                    IocContainer.BindToConstant(navigationService);
                }

                //Activating navigation provider if need
                INavigationProvider provider;
                IocContainer.TryGet(out provider);

                navigationService.UpdateRootPage(navigationPage, viewModel);
                mainPage = navigationPage;
            }
            Application.Current.MainPage = mainPage;

            NavigationMode mode = NavigationMode.New;

            if (isRoot)
            {
                if (viewModel.Settings.State.Contains(IsRootConstant))
                {
                    mode = NavigationMode.Refresh;
                }
                viewModel.Settings.State.AddOrUpdate(IsRootConstant, null);
            }
            IocContainer.Get <INavigationDispatcher>().OnNavigated(new NavigationContext(NavigationType.Page, mode, null, viewModel, this, context));
        }
        public virtual void OnNewIntent(Intent intent, Action <Intent> baseOnNewIntent)
        {
            baseOnNewIntent(intent);
            //note android doesn't support reorder activity by instance it will reorder it using type, we need to manually change view model if need
            var  oldViewModel = DataContext as IViewModel;
            Guid id;

            if (oldViewModel != null && Guid.TryParse(intent.GetStringExtra(IntentViewModelIdKey), out id))
            {
                var newViewModel = ServiceProvider.ViewModelProvider.TryGetViewModelById(id);
                if (newViewModel != null && !ReferenceEquals(newViewModel, oldViewModel))
                {
                    var navigationDispatcher = ServiceProvider.Get <INavigationDispatcher>();
                    var openedViewModels     = navigationDispatcher.GetOpenedViewModels(NavigationType.Page).ToList();
                    var viewModels           = openedViewModels
                                               .Select(info => info.ViewModel)
                                               .Where(vm => vm.GetCurrentView <object>()?.GetType() == Target.GetType())
                                               .ToList();

                    var from = viewModels.IndexOf(newViewModel);
                    var to   = viewModels.IndexOf(oldViewModel);

                    if (from < 0 || to < 0)
                    {
                        return;
                    }

                    for (int i = to; i > from; i--)
                    {
                        var viewModel   = viewModels[i];
                        var currentView = viewModels[i - 1].GetCurrentView <object>();
                        if (currentView != null)
                        {
                            ServiceProvider.ViewManager.InitializeViewAsync(viewModel, currentView);
                            var activity = currentView as Activity;
                            if (activity != null)
                            {
                                var newIntent = new Intent(activity.Intent);
                                newIntent.PutExtra(NavigationService.IntentParameterKey, NavigationProvider.GenerateNavigationParameter(viewModel));
                                activity.Intent = newIntent;
                            }
                        }
                    }
                    var newParameter = intent.GetStringExtra(NavigationService.IntentParameterKey);
                    var oldParameter = Target.Intent.GetStringExtra(NavigationService.IntentParameterKey);
                    if (newParameter != null && !string.Equals(newParameter, oldParameter))
                    {
                        var newIntent = new Intent(Target.Intent);
                        newIntent.PutExtra(NavigationService.IntentParameterKey, newParameter);
                        Target.Intent = newIntent;
                    }
                    ServiceProvider.ViewManager.InitializeViewAsync(newViewModel, Target);

                    int freeIndex = -1;
                    for (int i = 0; i < openedViewModels.Count; i++)
                    {
                        var viewModelInfo = openedViewModels[i];
                        if (freeIndex == -1 && ReferenceEquals(viewModelInfo.ViewModel, newViewModel))
                        {
                            ++i;
                            freeIndex = i;
                            openedViewModels.Insert(i, null);
                            continue;
                        }
                        if (freeIndex != -1 && viewModelInfo.ViewModel.GetCurrentView <object>()?.GetType() == Target.GetType())
                        {
                            openedViewModels[freeIndex] = viewModelInfo;
                            openedViewModels[i]         = null;
                            freeIndex = i;
                        }
                    }
                    if (freeIndex != -1)
                    {
                        openedViewModels.RemoveAt(freeIndex);
                        navigationDispatcher.UpdateOpenedViewModels(NavigationType.Page, openedViewModels);
                    }
                }
            }
        }