123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- namespace Caliburn.Micro.Core {
- using System;
- /// <summary>
- /// A result that executes an <see cref="System.Action"/>.
- /// </summary>
- public class DelegateResult : IResult {
- readonly Action toExecute;
- /// <summary>
- /// Initializes a new instance of the <see cref="DelegateResult"/> class.
- /// </summary>
- /// <param name="action">The action.</param>
- public DelegateResult(Action action) {
- toExecute = action;
- }
- /// <summary>
- /// Executes the result using the specified context.
- /// </summary>
- /// <param name="context">The context.</param>
- public void Execute(CoroutineExecutionContext context) {
- var eventArgs = new ResultCompletionEventArgs();
- try {
- toExecute();
- }
- catch (Exception ex) {
- eventArgs.Error = ex;
- }
- Completed(this, eventArgs);
- }
- /// <summary>
- /// Occurs when execution has completed.
- /// </summary>
- public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
- }
- /// <summary>
- /// A result that executes a <see cref="System.Func<TResult>"/>
- /// </summary>
- /// <typeparam name="TResult">The type of the result.</typeparam>
- public class DelegateResult<TResult> : IResult<TResult> {
- readonly Func<TResult> toExecute;
- /// <summary>
- /// Initializes a new instance of the <see cref="DelegateResult<TResult>"/> class.
- /// </summary>
- /// <param name="action">The action.</param>
- public DelegateResult(Func<TResult> action) {
- toExecute = action;
- }
- /// <summary>
- /// Executes the result using the specified context.
- /// </summary>
- /// <param name="context">The context.</param>
- public void Execute(CoroutineExecutionContext context) {
- var eventArgs = new ResultCompletionEventArgs();
- try {
- Result = toExecute();
- }
- catch (Exception ex) {
- eventArgs.Error = ex;
- }
- Completed(this, eventArgs);
- }
- /// <summary>
- /// Gets the result.
- /// </summary>
- public TResult Result { get; private set; }
- /// <summary>
- /// Occurs when execution has completed.
- /// </summary>
- public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
- }
- }
|