Пример #1
0
        private IWindowViewMediator TryCreateWindowViewMediator(IViewModel viewModel, IDataContext context)
        {
            bool data;

            if (context.TryGetData(NavigationConstants.SuppressWindowNavigation, out data) && data)
            {
                return(null);
            }

            var viewName = viewModel.GetViewName(context);
            IViewMappingItem mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewName, false);

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

            IWindowViewMediator viewMediator;

            if (!viewModel.Settings.Metadata.TryGetData(WindowViewMediatorConstant, out viewMediator))
            {
                viewMediator = CreateWindowViewMediator(viewModel, mappingItem.ViewType, context);
                if (viewMediator != null)
                {
                    viewModel.Settings.Metadata.Add(WindowViewMediatorConstant, viewMediator);
                }
            }
            return(viewMediator);
        }
Пример #2
0
        public virtual IAsyncOperation TryShowAsync(IDataContext context, IViewModelPresenter parentPresenter)
        {
            if (context.GetData(NavigationConstants.SuppressRootNavigation))
            {
                return(null);
            }

            var viewModel = context.GetData(NavigationConstants.ViewModel);

            if (viewModel == null || _hasRootPage)
            {
                return(null);
            }

            var mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewModel.GetViewName(context), false);

            if (mappingItem == null || !typeof(Page).IsAssignableFrom(mappingItem.ViewType))
            {
                return(null);
            }

            _hasRootPage = true;
            var operation = viewModel.RegisterNavigationOperation(OperationType.PageNavigation, context);

            ThreadManager.Invoke(ExecutionMode.AsynchronousOnUiThread, this, viewModel, context, (@this, model, arg3) => @this.InitializeRootPage(model, arg3));
            return(operation);
        }
Пример #3
0
        public virtual IAsyncOperation TryShowAsync(IDataContext context, IViewModelPresenter parentPresenter)
        {
            if (_initialized)
            {
                return(null);
            }

            if (context.GetData(NavigationConstants.SuppressRootNavigation))
            {
                return(null);
            }

            var viewModel = context.GetData(NavigationConstants.ViewModel);

            if (viewModel == null || Window == null)
            {
                return(null);
            }

            var mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewModel.GetViewName(context), false);

            if (mappingItem == null || !typeof(UIViewController).IsAssignableFrom(mappingItem.ViewType))
            {
                return(null);
            }

            _initialized = true;

            viewModel.Settings.State.AddOrUpdate(IsRootConstant, null);
            viewModel.Settings.Metadata.Add(ViewModelConstants.CanCloseHandler, (model, o) => false);
            var operation = viewModel.RegisterNavigationOperation(OperationType.PageNavigation, context);

            ThreadManager.Invoke(ExecutionMode.AsynchronousOnUiThread, this, viewModel, context, (@this, vm, ctx) => @this.InitializeRootView(vm, ctx));
            return(operation);
        }
        protected virtual void NavigateInternal(IViewModel viewModel, IDataContext context, TaskCompletionSource <bool> tcs)
        {
            //The view model is already shown as page and we need to bring it to front
            if (viewModel.Settings.State.Contains(IsNavigatedConstant))
            {
                context.AddOrUpdate(BringToFront, true);
            }

            context.AddOrUpdate(NavigatedTaskConstant, tcs);
            string viewName    = viewModel.GetViewName(context);
            var    mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewName, true);
            var    parameter   = GenerateNavigationParameter(viewModel);

            NavigationService.Navigate(mappingItem, parameter, context);
        }
        public Task NavigateAsync(IOperationCallback callback, IDataContext context)
        {
            Should.NotBeNull(context, nameof(context));
            IViewModel viewModel = context.GetData(NavigationConstants.ViewModel);

            if (viewModel == null)
            {
                throw new InvalidOperationException($"The '{GetType()}' provider doesn't support the DataContext without navigation target.");
            }
            context = context.ToNonReadOnly();
            if (ReferenceEquals(viewModel, CurrentViewModel))
            {
                if (callback != null)
                {
                    CallbackManager.Register(OperationType.PageNavigation, viewModel, callback, context);
                }
                return(Empty.Task);
            }
            //The view model is already shown as page and we need to bring it to front
            if (viewModel.Settings.State.Contains(IsNavigatedConstant))
            {
                context.AddOrUpdate(NavigationProviderConstants.BringToFront, true);
            }

            string viewName    = viewModel.GetViewName(context);
            var    vmType      = viewModel.GetType();
            var    mappingItem = ViewMappingProvider.FindMappingForViewModel(vmType, viewName, true);
            var    id          = Guid.NewGuid().ToString("n");
            var    parameter   = GenerateNavigationParameter(vmType, id);

            var tcs = new TaskCompletionSource <object>();

            CurrentNavigationTask.TryExecuteSynchronously(_ =>
                                                          ThreadManager.InvokeOnUiThreadAsync(() =>
            {
                _navigatedTcs       = tcs;
                _navigationTargetVm = viewModel;
                _currentCallback    = callback;
                _lastContext        = context;
                _currentOperationId = id;
                if (_navigationService.Navigate(mappingItem, parameter, context))
                {
                    ClearCacheIfNeed(context, viewModel);
                }
            }));
            return(tcs.Task);
        }
 private IViewMappingItem FindMappingForViewModel(Type viewModelType, string viewName)
 {
     return(ViewMappingProvider.FindMappingForViewModel(viewModelType, viewName, true));
 }