Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CommandBase.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 using SiliconStudio.Presentation.Services;
6 using SiliconStudio.Presentation.ViewModel;
7 
8 namespace SiliconStudio.Presentation.Commands
9 {
10  /// <summary>
11  /// An abstract class that is the base implementation of the <see cref="ICommandBase"/> interface.
12  /// </summary>
13  public abstract class CommandBase : DispatcherViewModel, ICommandBase
14  {
15  /// <summary>
16  /// Backing field for the <see cref="IsEnabled"/> property.
17  /// </summary>
18  private bool isEnabled = true;
19 
20  /// <summary>
21  /// Initializes a new instance of the <see cref="CommandBase"/> class.
22  /// </summary>
23  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
24  protected CommandBase(IViewModelServiceProvider serviceProvider)
25  : base(serviceProvider)
26  {
27  }
28 
29  /// <inheritdoc/>
30  public bool IsEnabled { get { return isEnabled; } set { SetValue(ref isEnabled, value, InvokeCanExecute); } }
31 
32  /// <inheritdoc/>
33  public event EventHandler CanExecuteChanged;
34 
35  /// <inheritdoc/>
36  public virtual bool CanExecute(object parameter)
37  {
38  return isEnabled;
39  }
40 
41  /// <inheritdoc/>
42  public abstract void Execute(object parameter);
43 
44  /// <summary>
45  /// Invokes <see cref="CanExecute(object)"/> with a <c>null</c> argument.
46  /// </summary>
47  /// <returns><c>true</c> if <see cref="CanExecute(object)"/> returned <c>true</c>, <c>false</c> otherwise.</returns>
48  public bool CanExecute()
49  {
50  return CanExecute(null);
51  }
52 
53  /// <summary>
54  /// Invokes the <see cref="Execute(object)"/> command with a <c>null</c> argument.
55  /// </summary>
56  public void Execute()
57  {
58  Execute(null);
59  }
60 
61  private void InvokeCanExecute()
62  {
63  var handler = CanExecuteChanged;
64  if (handler != null)
65  handler(this, EventArgs.Empty);
66  }
67  }
68 }
An interface representing an implementation of ICommand with additional properties.
Definition: ICommandBase.cs:10
CommandBase(IViewModelServiceProvider serviceProvider)
Initializes a new instance of the CommandBase class.
Definition: CommandBase.cs:24
virtual bool CanExecute(object parameter)
Definition: CommandBase.cs:36
bool CanExecute()
Invokes CanExecute(object) with a null argument.
Definition: CommandBase.cs:48
This abstract class is an implementation of ViewModelBase that uses a dispatcher to invoke OnProperty...
An abstract class that is the base implementation of the ICommandBase interface.
Definition: CommandBase.cs:13
void Execute()
Invokes the Execute(object) command with a null argument.
Definition: CommandBase.cs:56