/// <summary>
        /// Creates a window in non-modal state. If a window with the specified viewModelType exists, the window is activated instead of being created.
        /// </summary>
        /// <param name="uiVisualizerService">The UI visualizer service.</param>
        /// <typeparam name="TViewModel">The type of the view model.</typeparam>
        /// <param name="model">The model to be injected into the view model, can be <c>null</c>.</param>
        /// <param name="completedProc">The completed proc. Not applicable if window already exists.</param>
        /// <returns><c>true</c> if shown or activated successfully, <c>false</c> otherwise.</returns>
        public static bool?ShowOrActivate <TViewModel>(this IUIVisualizerService uiVisualizerService, object model = null, EventHandler <UICompletedEventArgs> completedProc = null)
            where TViewModel : IViewModel
        {
            Argument.IsNotNull("uiVisualizerService", uiVisualizerService);

            var dependencyResolver = uiVisualizerService.GetDependencyResolver();

            var viewModelManager = dependencyResolver.Resolve <IViewModelManager>();
            var viewModel        = viewModelManager.GetFirstOrDefaultInstance(typeof(TViewModel));

            if (viewModel == null)
            {
                var viewModelFactory = GetViewModelFactory(uiVisualizerService);
                var vm = viewModelFactory.CreateViewModel(typeof(TViewModel), model);
                return(uiVisualizerService.Show(vm, completedProc));
            }

            var viewLocator = dependencyResolver.Resolve <IViewLocator>();
            var viewType    = viewLocator.ResolveView(viewModel.GetType());
            var viewManager = dependencyResolver.Resolve <IViewManager>();
            var view        = viewManager.GetFirstOrDefaultInstance(viewType);
            var window      = view as System.Windows.Window;

            if (view == null || window == null)
            {
                return(uiVisualizerService.Show(viewModel, completedProc));
            }

            return(ActivateWindow(window));
        }
예제 #2
0
        public static Task <bool?> Show(this IUIVisualizerService @this, IViewModel viewModel, Action openedProc = null, EventHandler <UICompletedEventArgs> completedProc = null, uint timeOutInMilliseconds = 10000)
        {
            Argument.IsNotNull("@this", @this);

            return(new Task <bool?>(() =>
            {
                var innerTask = @this.Show(viewModel, completedProc);
                return innerTask.ContinueWith(t =>
                {
                    if ((t.Result ?? false) && openedProc != null)
                    {
                        var startTime = DateTime.Now;
                        ThreadPool.QueueUserWorkItem(state =>
                        {
                            var viewManager = ResolveTypeFromContainer <IViewManager>();
                            while (viewManager.GetViewsOfViewModel(viewModel).Length == 0 && DateTime.Now.Subtract(startTime).TotalMilliseconds < timeOutInMilliseconds)
                            {
                                ThreadHelper.Sleep(100);
                            }

                            var dispatcherService = ResolveTypeFromContainer <IDispatcherService>();
                            dispatcherService.Invoke(openedProc);
                        });
                    }

                    return t.Result;
                }).Result;
            }));
        }
예제 #3
0
        /// <summary>
        /// Execute in batch mode the enqueued tasks asynchronously.
        /// </summary>
        /// <param name="completedCallback">
        /// The completed callback.
        /// </param>
        /// <param name="viewModelType">
        /// The vie model type.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// If the batch is already committed and the execution is in progress or committing via async way.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The <paramref name="viewModelType"/> is not of type <see cref="IProgressNotifyableViewModel"/>.
        /// </exception>
        public void CommitAsync(Action completedCallback = null, Type viewModelType = null)
        {
            if (viewModelType != null)
            {
                Argument.IsOfType("viewModelType", viewModelType, typeof(IProgressNotifyableViewModel));
            }

            lock (_syncObj)
            {
                if (IsCommitting || IsRunning)
                {
                    throw new InvalidOperationException(ExecutionIsInProgressErrorMessage);
                }

                if (_tasks.Count == 0)
                {
                    throw new InvalidOperationException(AtLeastOneTaskShouldBeRegisteredErrorMessage);
                }

                _viewModelType = viewModelType;
                if (_viewModelType != null)
                {
                    _progressNotifyableViewModel = (IProgressNotifyableViewModel)_viewModelFactory.CreateViewModel(_viewModelType, null);
                    _uiVisualizerService.Show(_progressNotifyableViewModel);
                }
                else
                {
                    _progressNotifyableViewModel = null;
                }

                _thread = new Thread(() =>
                {
                    // NOTE: Patch for delay a bit the thread start
                    ThreadHelper.Sleep(100);
                    Execute();
                });

                _thread.SetApartmentState(ApartmentState.STA);
                _completedCallback = completedCallback;
                IsCommitting       = true;
            }

            _thread.Start();
        }
예제 #4
0
        /// <summary>
        /// Shows the window in non-modal state and creates the view model automatically using the specified model.
        /// </summary>
        /// <typeparam name="TViewModel">The type of the view model.</typeparam>
        /// <param name="uiVisualizerService">The UI visualizer service.</param>
        /// <param name="model">The model to be injected into the view model, can be <c>null</c>.</param>
        /// <param name="completedProc">The completed proc.</param>
        /// <returns><c>true</c> if shown successfully, <c>false</c> otherwise.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="uiVisualizerService" /> is <c>null</c>.</exception>
        public static bool?Show <TViewModel>(this IUIVisualizerService uiVisualizerService, object model = null, EventHandler <UICompletedEventArgs> completedProc = null)
            where TViewModel : IViewModel
        {
            Argument.IsNotNull("uiVisualizerService", uiVisualizerService);

            var viewModelFactory = GetViewModelFactory(uiVisualizerService);
            var vm = viewModelFactory.CreateViewModel(typeof(TViewModel), model);

            return(uiVisualizerService.Show(vm, completedProc));
        }
예제 #5
0
        /// <summary>
        /// Verifies the state of the service and also sets the commiting state to <c>true</c>.
        /// </summary>
        /// <param name="viewModelFunc">The view model instance.</param>
        /// <param name="show">Indicates whether the view model will be shown. If the view model is <c>null</c> then this argument will be ignored.</param>
        /// <exception cref="System.InvalidOperationException">
        /// </exception>
        /// <exception cref="InvalidOperationException">If the batch is already committed and the execution is in progress or committing via async way.</exception>
        private void BeginCommit(Func <IProgressNotifyableViewModel> viewModelFunc = null, bool show = true)
        {
            lock (_syncObj)
            {
                if (IsCommitting || IsRunning)
                {
                    throw new InvalidOperationException(ExecutionIsInProgressErrorMessage);
                }

                if (_tasks.Count == 0)
                {
                    throw new InvalidOperationException(AtLeastOneTaskShouldBeRegisteredErrorMessage);
                }

                _progressNotifyableViewModel = viewModelFunc == null ? null : viewModelFunc.Invoke();
                if (_progressNotifyableViewModel != null && show)
                {
                    _dispatcherService.Invoke(() => _uiVisualizerService.Show(_progressNotifyableViewModel));
                }

                IsCommitting = true;
            }
        }
예제 #6
0
        public static bool Show(this IUIVisualizerService @this, IViewModel viewModel, Action openedProc = null, EventHandler <UICompletedEventArgs> completedProc = null, uint timeOutInMilliseconds = 10000)
        {
            Argument.IsNotNull("@this", @this);

            bool result = @this.Show(viewModel, completedProc);

            if (result && openedProc != null)
            {
                DateTime startTime = DateTime.Now;
                ThreadPool.QueueUserWorkItem(state =>
                {
                    var viewManager = viewModel.GetService <IViewManager>();
                    while (viewManager.GetViewsOfViewModel(viewModel).Length == 0 && DateTime.Now.Subtract(startTime).TotalMilliseconds < timeOutInMilliseconds)
                    {
                        ThreadHelper.Sleep(100);
                    }

                    var dispatcherService = viewModel.GetService <IDispatcherService>();
                    dispatcherService.Invoke(openedProc);
                });
            }

            return(result);
        }
예제 #7
0
 private void OnShowLogWindowExecute()
 {
     _uiVisualizerService.Show <LogWindowViewModel>();
 }