示例#1
0
        public void PresentShouldInvokeCallbackOnClear()
        {
            var vm = GetViewModel <NavigableViewModelMock>();
            IOperationCallback operationCallback = null;

            ApplicationSettings.Platform      = new PlatformInfo(PlatformType.WPF, new Version(0, 0));
            OperationCallbackManager.Register = (type, o, arg3, arg4) =>
            {
                type.ShouldEqual(OperationType.TabNavigation);
                operationCallback = arg3;
            };
            OperationCallbackManager.SetResult = (o, result) =>
            {
                result.Operation.ShouldEqual(OperationType.TabNavigation);
                o.ShouldEqual(vm);
                operationCallback.Invoke(result);
            };

            vm.OperationResult = true;
            MultiViewModel             viewModel = GetMultiViewModel();
            IDynamicViewModelPresenter presenter = new DynamicMultiViewModelPresenter(viewModel,
                                                                                      OperationCallbackManager);
            IAsyncOperation <bool?> task = presenter.TryShowAsync(vm, DataContext.Empty, null);

            task.ShouldNotBeNull();
            task.IsCompleted.ShouldBeFalse();
            viewModel.Clear();
            task.IsCompleted.ShouldBeTrue();
            task.Result.Result.ShouldEqual(true);
            operationCallback.ShouldNotBeNull();
        }
示例#2
0
        public void PresentShouldInvokeCallbackOnRemove()
        {
            var vm = GetViewModel <NavigableViewModelMock>();
            IOperationCallback operationCallback = null;

            OperationCallbackManager.Register = (type, o, arg3, arg4) =>
            {
                type.ShouldEqual(OperationType.TabNavigation);
                operationCallback = arg3;
            };
            OperationCallbackManager.SetResult = (o, result) =>
            {
                result.Operation.ShouldEqual(OperationType.TabNavigation);
                o.ShouldEqual(vm);
                operationCallback.Invoke(result);
            };

            vm.OperationResult = true;
            MultiViewModel             viewModel = GetMultiViewModel();
            IDynamicViewModelPresenter presenter = new DynamicMultiViewModelPresenter(viewModel,
                                                                                      OperationCallbackManager, (model, context, arg3) => true);
            IAsyncOperation <bool?> task = presenter.TryShowAsync(vm, DataContext.Empty, null);

            task.ShouldNotBeNull();
            task.IsCompleted.ShouldBeFalse();
            viewModel.RemoveViewModelAsync(vm).Result.ShouldBeTrue();
            task.IsCompleted.ShouldBeTrue();
            task.Result.Result.ShouldEqual(true);
            operationCallback.ShouldNotBeNull();
        }
示例#3
0
        public Task ShowAsync(IOperationCallback callback, IDataContext context)
        {
            ViewModel.NotBeDisposed();
            if (IsOpen)
            {
                throw ExceptionManager.WindowOpened();
            }
            var tcs = new TaskCompletionSource <object>();

            RaiseNavigating(context, NavigationMode.New)
            .TryExecuteSynchronously(task =>
            {
                try
                {
                    if (!task.Result)
                    {
                        tcs.TrySetCanceled();
                        if (callback != null)
                        {
                            callback.Invoke(OperationResult.CreateCancelResult <bool?>(OperationType.WindowNavigation, ViewModel, context));
                        }
                        return;
                    }
                    if (context == null)
                    {
                        context = DataContext.Empty;
                    }
                    if (callback != null)
                    {
                        OperationCallbackManager.Register(OperationType.WindowNavigation, ViewModel, callback, context);
                    }
                    _isOpen = true;
                    ShowInternal(context, tcs);
                }
                catch (Exception e)
                {
                    tcs.TrySetException(e);
                    if (callback != null)
                    {
                        callback.Invoke(OperationResult.CreateErrorResult <bool?>(OperationType.WindowNavigation, ViewModel, e, context));
                    }
                }
            });
            return(tcs.Task);
        }
        /// <summary>
        ///     Navigates using the specified data context.
        /// </summary>
        /// <param name="callback">The specified callback, if any.</param>
        /// <param name="context">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        public void Navigate(IOperationCallback callback, IDataContext context)
        {
            Should.NotBeNull(context, "context");
            IViewModel viewModel = context.GetData(NavigationConstants.ViewModel);

            if (viewModel == null)
            {
                throw new InvalidOperationException(string.Format("The '{0}' provider doesn't support the DataContext without navigation target.", GetType()));
            }
            if (ReferenceEquals(viewModel, CurrentViewModel))
            {
                if (callback != null)
                {
                    callback.Invoke(OperationResult.CreateResult <bool?>(OperationType.Navigation, CurrentViewModel, true, context));
                }
                return;
            }

            string viewName   = viewModel.GetViewName(context);
            var    parameters = context.GetData(NavigationConstants.Parameters);

            var    vmType      = viewModel.GetType();
            var    mappingItem = FindMappingForViewModel(vmType, viewName);
            object parameter;

            if (parameters != null)
            {
                parameters = parameters.ToNonReadOnly();
                parameters.Add(VmTypeConstant, vmType.AssemblyQualifiedName);
                parameter = parameters;
            }
            else
            {
                parameter = vmType.AssemblyQualifiedName;
            }

            ThreadManager.InvokeOnUiThreadAsync(() =>
            {
                CancelCurrentNavigation(null);
                _navigationTargetVm = viewModel;
                _currentCallback    = callback;
                _navigationService.Navigate(mappingItem, parameter, context);
            });
        }
        public Task NavigateAsync(IOperationCallback callback, IDataContext context)
        {
            Should.NotBeNull(context, "context");
            IViewModel viewModel = context.GetData(NavigationConstants.ViewModel);

            if (viewModel == null)
            {
                throw new InvalidOperationException(string.Format("The '{0}' provider doesn't support the DataContext without navigation target.", GetType()));
            }
            if (ReferenceEquals(viewModel, CurrentViewModel))
            {
                if (callback != null)
                {
                    callback.Invoke(OperationResult.CreateResult <bool?>(OperationType.PageNavigation, CurrentViewModel, true, context));
                }
                return(Empty.Task);
            }

            string viewName    = viewModel.GetViewName(context);
            var    vmType      = viewModel.GetType();
            var    mappingItem = FindMappingForViewModel(vmType, viewName);
            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);
        }
        /// <summary>
        ///     Navigates using the specified data context.
        /// </summary>
        /// <param name="callback">The specified callback, if any.</param>
        /// <param name="context">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        public Task NavigateAsync(IOperationCallback callback, IDataContext context)
        {
            Should.NotBeNull(context, "context");
            IViewModel viewModel = context.GetData(NavigationConstants.ViewModel);
            if (viewModel == null)
                throw new InvalidOperationException(string.Format("The '{0}' provider doesn't support the DataContext without navigation target.", GetType()));
            if (ReferenceEquals(viewModel, CurrentViewModel))
            {
                if (callback != null)
                    callback.Invoke(OperationResult.CreateResult<bool?>(OperationType.PageNavigation, CurrentViewModel, true, context));
                return Empty.Task;
            }

            string viewName = viewModel.GetViewName(context);
            var vmType = viewModel.GetType();
            var mappingItem = FindMappingForViewModel(vmType, viewName);
            var id = Guid.NewGuid().ToString("n");
            var parameter = vmType.AssemblyQualifiedName + IdSeparator[0] + 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;
        }