Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added helper methods for lazy command creation.

  • Loading branch information...
commit 527041ca44d16e7d761b94e07df92b5a86bcbf4a 1 parent 006e4a4
@jlaanstra authored
Showing with 103 additions and 0 deletions.
  1. +103 −0 ReactiveUI.Xaml/ReactiveCommand.cs
View
103 ReactiveUI.Xaml/ReactiveCommand.cs
@@ -89,6 +89,31 @@ protected ReactiveCommand(Func<object, bool> canExecute, IScheduler scheduler =
}
/// <summary>
+ /// Creates a new ReactiveCommand object in an Rx way,
+ /// similar to RelayCommand.
+ /// </summary>
+ /// <param name="canExecute">A function that determines when the Command
+ /// can execute.</param>
+ /// <param name="executed">A method that will be invoked when the
+ /// Execute method is invoked.</param>
+ /// <param name="scheduler">The scheduler to publish events on - default
+ /// is RxApp.DeferredScheduler.</param>
+ /// <returns>A new ReactiveCommand object.</returns>
+ public static ReactiveCommand Create(
+ IObservable<bool> canExecute,
+ Action<object> executed = null,
+ IScheduler scheduler = null)
+ {
+ var ret = new ReactiveCommand(canExecute, scheduler);
+ if (executed != null)
+ {
+ ret.Subscribe(executed);
+ }
+
+ return ret;
+ }
+
+ /// <summary>
/// Creates a new ReactiveCommand object in an imperative, non-Rx way,
/// similar to RelayCommand, only via a TPL Async method
/// </summary>
@@ -112,6 +137,84 @@ protected ReactiveCommand(Func<object, bool> canExecute, IScheduler scheduler =
return ret;
}
+ /// <summary>
+ /// Lazy creates a new ReactiveCommand object in an imperative, non-Rx way,
+ /// similar to RelayCommand, only if the command was not yet created.
+ /// Otherwise just returns the value of the backing field.
+ /// </summary>
+ /// <param name="backingCommand">The backing field for this Command.</param>
+ /// <param name="canExecute">A function that determines when the Command
+ /// can execute.</param>
+ /// <param name="executed">A method that will be invoked when the
+ /// Execute method is invoked.</param>
+ /// <param name="scheduler">The scheduler to publish events on - default
+ /// is RxApp.DeferredScheduler.</param>
+ /// <returns>A new ReactiveCommand object.</returns>
+ public static ReactiveCommand CreateIfNull(
+ ref ReactiveCommand backingCommand,
+ Func<object, bool> canExecute,
+ Action<object> executed = null,
+ IScheduler scheduler = null)
+ {
+ if (backingCommand != null)
+ {
+ return backingCommand;
+ }
+ return ReactiveCommand.Create(canExecute, executed, scheduler);
+ }
+
+ /// <summary>
+ /// Lazy creates a new ReactiveCommand object in an Rx way,
+ /// similar to RelayCommand, only if the command was not yet created.
+ /// Otherwise just returns the value of the backing field.
+ /// </summary>
+ /// <param name="backingCommand">The backing field for this Command.</param>
+ /// <param name="canExecute">A function that determines when the Command
+ /// can execute.</param>
+ /// <param name="executed">A method that will be invoked when the
+ /// Execute method is invoked.</param>
+ /// <param name="scheduler">The scheduler to publish events on - default
+ /// is RxApp.DeferredScheduler.</param>
+ /// <returns>A new ReactiveCommand object.</returns>
+ public static ReactiveCommand CreateIfNull(
+ ref ReactiveCommand backingCommand,
+ IObservable<bool> canExecute = null,
+ Action<object> executed = null,
+ IScheduler scheduler = null)
+ {
+ if (backingCommand != null)
+ {
+ return backingCommand;
+ }
+ return ReactiveCommand.Create(canExecute, executed, scheduler);
+ }
+
+ /// <summary>
+ /// Lazy creates a new ReactiveCommand object in an imperative, non-Rx way,
+ /// similar to RelayCommand, only via a TPL Async method and if the command
+ /// was not yet created. Otherwise just returns the value of the backing field.
+ /// </summary>
+ /// <param name="backingCommand">The backing field for this Command.</param>
+ /// <param name="canExecute">A function that determines when the Command
+ /// can execute.</param>
+ /// <param name="executed">A method that will be invoked when the
+ /// Execute method is invoked.</param>
+ /// <param name="scheduler">The scheduler to publish events on - default
+ /// is RxApp.DeferredScheduler.</param>
+ /// <returns>A new ReactiveCommand object.</returns>
+ public static ReactiveCommand CreateIfNull(
+ ref ReactiveCommand backingCommand,
+ Func<object, Task<bool>> canExecute,
+ Action<object> executed = null,
+ IScheduler scheduler = null)
+ {
+ if (backingCommand != null)
+ {
+ return backingCommand;
+ }
+ return ReactiveCommand.Create(canExecute, executed, scheduler);
+ }
+
public IObservable<Exception> ThrownExceptions { get; protected set; }
void commonCtor(IScheduler scheduler)
Please sign in to comment.
Something went wrong with that request. Please try again.