CommunityToolkit / CommunityToolkit/dotnet
RelayCommand and AsyncRelayCommand - CanExecuteChanged and PropertyChanged events on main thread
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### 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 and AsyncRelayCommand.
### API breakdown
```csharp
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
```csharp
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:
```csharp
public interface IDispatcher
{
void InvokeOnMainThread(Action action);
}
namespace CommunityToolkit.Mvvm.Input;
public sealed class RelayCommand : IRelayCommand
{
private static readonly Lazy _dispatcher = new Lazy(() => Ioc.Default.GetService());
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
Contributor guide
Assessment
This issue has not been assessed yet.