1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-04-10 11:15:45 +02:00

Code refactoring, added the IRelayCommand interface

This commit is contained in:
Sergio Pedri 2020-04-16 21:12:46 +02:00
parent d40c3ae642
commit bcbb8c9eab
6 changed files with 31 additions and 21 deletions

View File

@ -5,7 +5,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Toolkit.Mvvm.Commands.Interfaces;
using Microsoft.Toolkit.Mvvm.ComponentModel;
namespace Microsoft.Toolkit.Mvvm.Commands
@ -16,7 +16,7 @@ namespace Microsoft.Toolkit.Mvvm.Commands
/// action, and providing a <see cref="ExecutionTask"/> property that notifies changes when
/// <see cref="Execute"/> is invoked and when the returned <see cref="Task{TResult}"/> completes.
/// </summary>
public sealed class AsyncRelayCommand : ObservableObject, ICommand
public sealed class AsyncRelayCommand : ObservableObject, IRelayCommand
{
/// <summary>
/// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute"/> is used.
@ -63,9 +63,7 @@ public Task? ExecutionTask
private set => SetAndNotifyOnCompletion(ref executionTask, () => executionTask, value);
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
/// <inheritdoc/>
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);

View File

@ -5,6 +5,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.Commands.Interfaces;
using Microsoft.Toolkit.Mvvm.ComponentModel;
namespace Microsoft.Toolkit.Mvvm.Commands
@ -16,7 +17,7 @@ namespace Microsoft.Toolkit.Mvvm.Commands
/// <see cref="Execute(T)"/> is invoked and when the returned <see cref="Task{TResult}"/> completes.
/// </summary>
/// <typeparam name="T">The type of parameter being passed as input to the callbacks.</typeparam>
public sealed class AsyncRelayCommand<T> : ObservableObject, ICommand<T>
public sealed class AsyncRelayCommand<T> : ObservableObject, IRelayCommand<T>
{
/// <summary>
/// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute(T)"/> is used.
@ -63,9 +64,7 @@ public Task? ExecutionTask
private set => SetAndNotifyOnCompletion(ref executionTask, () => executionTask, value);
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
/// <inheritdoc/>
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);

View File

@ -0,0 +1,16 @@
using System.Windows.Input;
namespace Microsoft.Toolkit.Mvvm.Commands.Interfaces
{
/// <summary>
/// An interface expanding <see cref="ICommand"/> with the ability to raise
/// the <see cref="ICommand.CanExecuteChanged"/> event externally.
/// </summary>
public interface IRelayCommand : ICommand
{
/// <summary>
/// Raises the <see cref="ICommand.CanExecuteChanged" /> event.
/// </summary>
void RaiseCanExecuteChanged();
}
}

View File

@ -1,12 +1,12 @@
using System.Windows.Input;
namespace Microsoft.Toolkit.Mvvm.Commands
namespace Microsoft.Toolkit.Mvvm.Commands.Interfaces
{
/// <summary>
/// A generic interface representing a more specific version of <see cref="ICommand"/>.
/// A generic interface representing a more specific version of <see cref="IRelayCommand"/>.
/// </summary>
/// <typeparam name="T">The type used as argument for the interface methods.</typeparam>
public interface ICommand<in T> : ICommand
public interface IRelayCommand<in T> : IRelayCommand
{
/// <summary>
/// Provides a strongly-typed variant of <see cref="ICommand.CanExecute(object)"/>.

View File

@ -23,7 +23,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Toolkit.Mvvm.Commands.Interfaces;
namespace Microsoft.Toolkit.Mvvm.Commands
{
@ -33,7 +33,7 @@ namespace Microsoft.Toolkit.Mvvm.Commands
/// method is <see langword="true"/>. This type does not allow you to accept command parameters
/// in the <see cref="Execute"/> and <see cref="CanExecute"/> callback methods.
/// </summary>
public sealed class RelayCommand : ICommand
public sealed class RelayCommand : IRelayCommand
{
/// <summary>
/// The <see cref="Action"/> to invoke when <see cref="Execute"/> is used.
@ -68,9 +68,7 @@ public RelayCommand(Action execute, Func<bool> canExecute)
this.canExecute = canExecute;
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
/// <inheritdoc/>
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);

View File

@ -23,6 +23,7 @@
using System;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Mvvm.Commands.Interfaces;
namespace Microsoft.Toolkit.Mvvm.Commands
{
@ -33,7 +34,7 @@ namespace Microsoft.Toolkit.Mvvm.Commands
/// in the <see cref="Execute(T)"/> and <see cref="CanExecute(T)"/> callback methods.
/// </summary>
/// <typeparam name="T">The type of parameter being passed as input to the callbacks.</typeparam>
public sealed class RelayCommand<T> : ICommand<T>
public sealed class RelayCommand<T> : IRelayCommand<T>
{
/// <summary>
/// The <see cref="Action"/> to invoke when <see cref="Execute(T)"/> is used.
@ -68,9 +69,7 @@ public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
this.canExecute = canExecute;
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
/// <inheritdoc/>
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);