Exemplo n.º 1
0
        /// <summary>
        /// Executes the result using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            var openFileDialog = new OpenFileDialog();

            openFileDialog.RestoreDirectory = true;
            openFileDialog.CheckFileExists  = true;
            openFileDialog.CheckPathExists  = true;

            openFileDialog.Multiselect      = _multiselect;
            openFileDialog.Filter           = _fileTypeFilter;
            openFileDialog.Title            = _title;
            openFileDialog.InitialDirectory = _initialDirectory;

            var activeWindow = CoTaskHelper.GetActiveWindow(context);

            bool fileSelected;

            try
            {
                fileSelected = openFileDialog.ShowDialog(activeWindow).GetValueOrDefault();
            }
            catch
            {
                if (string.IsNullOrEmpty(openFileDialog.InitialDirectory))
                {
                    throw;
                }
                openFileDialog.InitialDirectory = null;
                fileSelected = openFileDialog.ShowDialog(activeWindow).GetValueOrDefault();
            }

            OnCompleted(openFileDialog, new CoTaskCompletedEventArgs(null, !fileSelected));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves the value of this instance.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>The resolved value.</returns>
        public object Resolve(CoroutineExecutionContext context)
        {
            var sourceOverride = SourceOverride;
            if (sourceOverride != null)
                context.Source = sourceOverride;

            return context;
        }
        /// <summary>
        /// Executes the coTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override async void BeginExecute(CoroutineExecutionContext context)
        {
            try
            {
                await _innerTask;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }

            OnCompleted(_innerTask);
        }
        /// <summary>
        /// Executes the coTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override async void BeginExecute(CoroutineExecutionContext context)
        {
            try
            {
                await _innerTask;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }

            OnCompleted(_innerTask);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Called when the execution of the decorated CoTask has completed.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="innerCoTask">The decorated CoTask.</param>
 /// <param name="args">The <see cref="CoTaskCompletedEventArgs" /> instance containing the event data.</param>
 protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
     CoTaskCompletedEventArgs args)
 {
     if (args.Error != null || !args.WasCancelled)
     {
         OnCompleted(new CoTaskCompletedEventArgs(args.Error, false));
     }
     else
     {
         Continue(context);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Executes the CoTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            _context = context;

            try
            {
                _innerCoTask.Completed += InnerCoTaskCompleted;
                _innerCoTask.BeginExecute(_context);
            }
            catch (Exception ex)
            {
                InnerCoTaskCompleted(_innerCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Executes the CoTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            _context = context;

            try
            {
                _innerCoTask.Completed += InnerCoTaskCompleted;
                _innerCoTask.BeginExecute(_context);
            }
            catch (Exception ex)
            {
                InnerCoTaskCompleted(_innerCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
        /// <summary>
        /// Called when the execution of the decorated CoTask has completed.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="innerCoTask">The decorated coTask.</param>
        /// <param name="args">The <see cref="Caliburn.Light.CoTaskCompletedEventArgs" /> instance containing the event data.</param>
        protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
                                                       CoTaskCompletedEventArgs args)
        {
            var error = args.Error as TException;

            if (error == null)
            {
                OnCompleted(args);
            }
            else
            {
                Rescue(context, error);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Executes the CoTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            Exception error = null;

            try
            {
                _toExecute();
            }
            catch (Exception ex)
            {
                error = ex;
            }

            OnCompleted(new CoTaskCompletedEventArgs(error, false));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Executes the CoTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            Exception error = null;

            try
            {
                _toExecute();
            }
            catch (Exception ex)
            {
                error = ex;
            }

            OnCompleted(new CoTaskCompletedEventArgs(error, false));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Executes the result using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            var activeWindow = CoTaskHelper.GetActiveWindow(context);

            if (activeWindow != null)
            {
                Result = MessageBox.Show(activeWindow, _message, _caption, _button, _image);
            }
            else
            {
                Result = MessageBox.Show(_message, _caption, _button, _image);
            }

            OnCompleted(new CoTaskCompletedEventArgs(null, false));
        }
Exemplo n.º 12
0
        private object[] DetermineParameters(MethodBase method, object eventArgs = null)
        {
            var requiredParameters = method.GetParameters();

            if (requiredParameters.Length == 0)
            {
                return(new object[0]);
            }

            var parameterValues = Parameters.Select(x => ((Parameter)x).Value).ToArray();

            if (requiredParameters.Length != parameterValues.Length)
            {
                throw new InvalidOperationException(string.Format("Inconsistent number of parameters for method {0}.", method.Name));
            }

            var context = (CoroutineExecutionContext)null;

            for (var i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType  = requiredParameters[i].ParameterType;
                var parameterValue = parameterValues[i];

                var specialValue = parameterValue as ISpecialValue;
                if (specialValue != null)
                {
                    if (context == null)
                    {
                        context = new CoroutineExecutionContext
                        {
                            Source    = AssociatedObject,
                            Target    = Target,
                            EventArgs = eventArgs,
                        }
                    }
                    ;

                    parameterValue = specialValue.Resolve(context);
                }

                parameterValues[i] = ParameterBinder.CoerceValue(parameterType, parameterValue);
            }

            return(parameterValues);
        }
Exemplo n.º 13
0
        private static object DetermineParameter(object sender, object eventArgs)
        {
            var resolvedParameter = UIContext.GetCommandParameter(sender);

            var specialValue = resolvedParameter as ISpecialValue;

            if (specialValue != null)
            {
                var context = new CoroutineExecutionContext
                {
                    Source    = sender,
                    EventArgs = eventArgs,
                };
                resolvedParameter = specialValue.Resolve(context);
            }

            return(resolvedParameter ?? eventArgs);
        }
Exemplo n.º 14
0
        private object DetermineParameter(object eventArgs = null)
        {
            var resolvedParameter = CommandParameter;

            var specialValue = resolvedParameter as ISpecialValue;

            if (specialValue != null)
            {
                var context = new CoroutineExecutionContext
                {
                    Source    = AssociatedObject,
                    EventArgs = eventArgs,
                };
                resolvedParameter = specialValue.Resolve(context);
            }

            return(resolvedParameter ?? eventArgs);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Executes the result using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            var saveFileDialog = new SaveFileDialog();

            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.AddExtension     = true;
            saveFileDialog.CheckPathExists  = true;

            saveFileDialog.Filter     = _fileTypeFilter;
            saveFileDialog.DefaultExt = _defaultFileExtension;

            saveFileDialog.Title            = _title;
            saveFileDialog.FileName         = _fileName;
            saveFileDialog.CreatePrompt     = _promptForCreate;
            saveFileDialog.OverwritePrompt  = _promptForOverwrite;
            saveFileDialog.InitialDirectory = _initialDirectory;

            var activeWindow = CoTaskHelper.GetActiveWindow(context);

            bool fileSelected;

            try
            {
                fileSelected = saveFileDialog.ShowDialog(activeWindow).GetValueOrDefault();
            }
            catch
            {
                if (string.IsNullOrEmpty(saveFileDialog.InitialDirectory))
                {
                    throw;
                }
                saveFileDialog.InitialDirectory = null;
                fileSelected = saveFileDialog.ShowDialog(activeWindow).GetValueOrDefault();
            }

            if (fileSelected)
            {
                Result = new FileInfo(saveFileDialog.FileName);
            }

            OnCompleted(new CoTaskCompletedEventArgs(null, !fileSelected));
        }
Exemplo n.º 16
0
        private static Task <TResult> InternalExecuteAsync <TResult>(ICoTask coTask, CoroutineExecutionContext context)
        {
            var taskSource = new TaskCompletionSource <TResult>();

            EventHandler <CoTaskCompletedEventArgs> completed = null;

            completed = (s, e) =>
            {
                ((ICoTask)s).Completed -= completed;

                if (e.Error != null)
                {
                    taskSource.TrySetException(e.Error);
                }
                else if (e.WasCancelled)
                {
                    taskSource.TrySetCanceled();
                }
                else
                {
                    var rr = s as ICoTask <TResult>;
                    taskSource.TrySetResult(rr != null ? rr.Result : default(TResult));
                }
            };

            try
            {
                coTask.Completed += completed;
                coTask.BeginExecute(context ?? new CoroutineExecutionContext());
            }
            catch (Exception ex)
            {
                coTask.Completed -= completed;
                taskSource.TrySetException(ex);
            }

            return(taskSource.Task);
        }
Exemplo n.º 17
0
        private void Continue(CoroutineExecutionContext context)
        {
            ICoTask continueCoTask;
            try
            {
                continueCoTask = _coroutine();
            }
            catch (Exception ex)
            {
                OnCompleted(new CoTaskCompletedEventArgs(ex, false));
                return;
            }

            try
            {
                continueCoTask.Completed += ContinueCompleted;
                continueCoTask.BeginExecute(context);
            }
            catch (Exception ex)
            {
                ContinueCompleted(continueCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
        private void Rescue(CoroutineExecutionContext context, TException exception)
        {
            ICoTask rescueCoTask;

            try
            {
                rescueCoTask = _coroutine(exception);
            }
            catch (Exception ex)
            {
                OnCompleted(new CoTaskCompletedEventArgs(ex, false));
                return;
            }

            try
            {
                rescueCoTask.Completed += RescueCompleted;
                rescueCoTask.BeginExecute(context);
            }
            catch (Exception ex)
            {
                RescueCompleted(rescueCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
Exemplo n.º 19
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public override void BeginExecute(CoroutineExecutionContext context)
 {
     OnCompleted(new CoTaskCompletedEventArgs(_error, _wasCancelled));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public override void BeginExecute(CoroutineExecutionContext context)
 {
     OnCompleted(new CoTaskCompletedEventArgs(_error, _wasCancelled));
 }
Exemplo n.º 21
0
 private void InnerCoTaskCompleted(object sender, CoTaskCompletedEventArgs args)
 {
     _innerCoTask.Completed -= InnerCoTaskCompleted;
     OnInnerResultCompleted(_context, _innerCoTask, args);
     _context = null;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Called when the execution of the decorated CoTask has completed.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="innerCoTask">The decorated CoTask.</param>
 /// <param name="args">The <see cref="CoTaskCompletedEventArgs"/> instance containing the event data.</param>
 protected abstract void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
     CoTaskCompletedEventArgs args);
Exemplo n.º 23
0
 /// <summary>
 /// Called when the execution of the decorated CoTask has completed.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="innerCoTask">The decorated CoTask.</param>
 /// <param name="args">The <see cref="CoTaskCompletedEventArgs"/> instance containing the event data.</param>
 protected abstract void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
                                                CoTaskCompletedEventArgs args);
Exemplo n.º 24
0
 /// <summary>
 /// Executes an <see cref="ICoTask&lt;TResult&gt;"/> asynchronous.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="coTask">The coroutine to execute.</param>
 /// <param name="context">The context to execute the coroutine within.</param>
 /// <returns>A task that represents the asynchronous coroutine.</returns>
 public static Task <TResult> ExecuteAsync <TResult>(this ICoTask <TResult> coTask,
                                                     CoroutineExecutionContext context = null)
 {
     return(InternalExecuteAsync <TResult>(coTask, context));
 }
Exemplo n.º 25
0
        private static Window GetWindowFromContext(CoroutineExecutionContext context)
        {
            var view = context.Source as DependencyObject;

            return(view != null?Window.GetWindow(view) : null);
        }
Exemplo n.º 26
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public abstract void BeginExecute(CoroutineExecutionContext context);
Exemplo n.º 27
0
 private void InnerCoTaskCompleted(object sender, CoTaskCompletedEventArgs args)
 {
     _innerCoTask.Completed -= InnerCoTaskCompleted;
     OnInnerResultCompleted(_context, _innerCoTask, args);
     _context = null;
 }
Exemplo n.º 28
0
 private void OnComplete(Exception error, bool wasCancelled)
 {
     _context = null;
     _enumerator.Dispose();
     OnCompleted(new CoTaskCompletedEventArgs(error, wasCancelled));
 }
Exemplo n.º 29
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public override void BeginExecute(CoroutineExecutionContext context)
 {
     _context = context;
     ChildCompleted(null, new CoTaskCompletedEventArgs(null, false));
 }
Exemplo n.º 30
0
 public static Window GetActiveWindow(CoroutineExecutionContext context)
 {
     return GetWindowFromContext(context) ?? GetFirstActiveWindow();
 }
Exemplo n.º 31
0
 public object Resolve(CoroutineExecutionContext context)
 {
     return ((ItemClickEventArgs)context.EventArgs).ClickedItem;
 }
Exemplo n.º 32
0
 /// <summary>
 /// Executes an <see cref="ICoTask"/> asynchronous.
 /// </summary>
 /// <param name="coTask">The coroutine to execute.</param>
 /// <param name="context">The context to execute the coroutine within.</param>
 /// <returns>A task that represents the asynchronous coroutine.</returns>
 public static Task ExecuteAsync(this ICoTask coTask, CoroutineExecutionContext context = null)
 {
     return(InternalExecuteAsync <object>(coTask, context));
 }
Exemplo n.º 33
0
 public static Window GetActiveWindow(CoroutineExecutionContext context)
 {
     return(GetWindowFromContext(context) ?? GetFirstActiveWindow());
 }
Exemplo n.º 34
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public override void BeginExecute(CoroutineExecutionContext context)
 {
     _context = context;
     ChildCompleted(null, new CoTaskCompletedEventArgs(null, false));
 }
Exemplo n.º 35
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public abstract void BeginExecute(CoroutineExecutionContext context);
Exemplo n.º 36
0
 private void OnComplete(Exception error, bool wasCancelled)
 {
     _context = null;
     _enumerator.Dispose();
     OnCompleted(new CoTaskCompletedEventArgs(error, wasCancelled));
 }
Exemplo n.º 37
0
 private static Window GetWindowFromContext(CoroutineExecutionContext context)
 {
     var view = context.Source as DependencyObject;
     return view != null ? Window.GetWindow(view) : null;
 }