dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

RelayCommand and AsyncRelayCommand - CanExecuteChanged and PropertyChanged events on main thread

Open KrzysztofFryzlewicz opened this issue 2 years ago • 1 comments

Overview

In Xamarin bindings to UI elements have to be invoked on main thread no matter what context the changes were invoked on. Proposed solution is to add virtual methods that encapsulate CanExecuteChanged (both AsyncRelayCommand and RelayCommand) and PropertyChanged (AsyncRelayCommand) invocation, similarly to ObservableObject.OnPropertyChanged or ObservableObject.OnPropertyChanging. Currently there is NotifyCanExecuteChanged method, but not virtual and it is only used once AsyncRelayCommand (should be for all usages of this event). Also both classes should not be sealed to enable inheritance. Similarly for RelayCommand<T> and AsyncRelayCommand<T>.

API breakdown

namespace CommunityToolkit.Mvvm.Input;

public class RelayCommand : IRelayCommand
{
    public virtual void NotifyCanExecuteChanged()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }
}

public class AsyncRelayCommand : IAsyncRelayCommand, ICancellationAwareCommand
{
    public virtual void NotifyCanExecuteChanged()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        ArgumentNullException.ThrowIfNull(e);

        PropertyChanged?.Invoke(this, e);
    }
}

Usage example

public class DerivedRelayCommand : RelayCommand
{
    public override NotifyCanExecuteChanged()
    {
        SomeMainThreadDispatcher.InvokeOnMainThread(() => base.NotifyCanExecuteChanged());
    }
}

public class DerivedAsyncRelayCommand : AsyncRelayCommand
{
    public override NotifyCanExecuteChanged()
    {
        SomeMainThreadDispatcher.InvokeOnMainThread(() => base.NotifyCanExecuteChanged());
    }

    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        SomeMainThreadDispatcher.InvokeOnMainThread(() => base.OnPropertyChanged(e));
    }
}

Breaking change?

No

Alternatives

Introducing static field with dispatcher resolved by Ioc:

public interface IDispatcher
{
    void InvokeOnMainThread(Action action);
}

namespace CommunityToolkit.Mvvm.Input;

public sealed class RelayCommand : IRelayCommand
{
    private static readonly Lazy<IDispatcher> _dispatcher = new Lazy<IDispatcher>(() => Ioc.Default.GetService<IDispatcher>());

    public void NotifyCanExecuteChanged()
    {
        if (_dispatcher.Value is IDispatcher dispatcher)
        {
            dispatcher.InvokeOnMainThread(() => CanExecuteChanged?.Invoke(this, EventArgs.Empty));
            return;
        } 

        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }
}

Additional context

No response

Help us help you

Yes, but only if others can assist

KrzysztofFryzlewicz avatar Oct 27 '23 07:10 KrzysztofFryzlewicz

seems duplicate with: https://github.com/CommunityToolkit/dotnet/issues/536

Rand-Random avatar Feb 01 '24 17:02 Rand-Random