//
// Copyright © GalaSoft Laurent Bugnion 2009-2013
//
// ****************************************************************************
// Laurent Bugnion
// laurent@galasoft.ch
// 22.4.2009
// BioNano.Common.Framework.Mvvm
// http://www.galasoft.ch
//
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
//
// BL0011
// ****************************************************************************
// This class was developed by Josh Smith (http://joshsmithonwpf.wordpress.com) and
// slightly modified with his permission.
// ****************************************************************************
using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Input;
////using GalaSoft.Utilities.Attributes;
namespace Bro.Common.ImageCanvas
{
///
/// A command whose sole purpose is to relay its functionality to other
/// objects by invoking delegates. The default return value for the CanExecute
/// method is 'true'. This class does not allow you to accept command parameters in the
/// Execute and CanExecute callback methods.
///
////[ClassInfo(typeof(RelayCommand),
//// Description = "A command whose sole purpose is to relay its functionality to other objects by invoking delegates.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
internal class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func _canExecute;
///
/// Initializes a new instance of the RelayCommand class that
/// can always execute.
///
/// The execution logic.
/// If the execute argument is null.
public RelayCommand(Action execute)
: this(execute, null)
{
}
///
/// Initializes a new instance of the RelayCommand class.
///
/// The execution logic.
/// The execution status logic.
/// If the execute argument is null.
public RelayCommand(Action execute, Func canExecute)
{
if (execute == null)
{
throw new ArgumentNullException(nameof(execute));
}
_execute = execute;
if (canExecute != null)
{
_canExecute = canExecute;
}
}
#if SILVERLIGHT
///
/// Occurs when changes occur that affect whether the command should execute.
///
public event EventHandler CanExecuteChanged;
#else
#if NETFX_CORE
///
/// Occurs when changes occur that affect whether the command should execute.
///
public event EventHandler CanExecuteChanged;
#else
#if XAMARIN
///
/// Occurs when changes occur that affect whether the command should execute.
///
public event EventHandler CanExecuteChanged;
#else
///
/// Occurs when changes occur that affect whether the command should execute.
///
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
CommandManager.RequerySuggested += value;
}
remove
{
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
}
}
#endif
#endif
#endif
///
/// Raises the event.
///
[SuppressMessage(
"Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "The this keyword is used in the Silverlight version")]
[SuppressMessage(
"Microsoft.Design",
"CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
public void RaiseCanExecuteChanged()
{
#if SILVERLIGHT
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
#else
#if NETFX_CORE
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
#else
#if XAMARIN
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
#else
CommandManager.InvalidateRequerySuggested();
#endif
#endif
#endif
}
///
/// Defines the method that determines whether the command can execute in its current state.
///
/// This parameter will always be ignored.
/// true if this command can be executed; otherwise, false.
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute();
}
///
/// Defines the method to be called when the command is invoked.
///
/// This parameter will always be ignored.
public virtual void Execute(object parameter)
{
if (CanExecute(parameter))
_execute?.Invoke();
}
}
}