Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What class do I use to "new" a command in Avalonia?

What do I have to new to instantiate a command in Avalonia? I don’t want to use any libraries for that. No ReativeUI, no CommunityToolkit etc. I know I need some methods that will implement the Execute method, but just in order to start with commands, what do I need to use?

using System.Windows.Input;

public class HomeModel
{
    public HomeModel()
    {
        OpenBrowserCommand = new TODO();
    }

    public ICommand OpenBrowserCommand { get; }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A good implementation of ICommand can be found in ReactiveUI’s ReactiveCommand. If you’ve created your application using the Avalonia MVVM Application template then this will be available by default. See the ReactiveUI documentation for more information.

public class HomeModel
{
    public HomeModel()
    {
        OpenBrowserCommand = ReactiveCommand.Create(RunTheThing);
        // or
        OpenBrowserCommand = ReactiveCommand.Create<string>(RunTheThingWithParameter);
    }

    public ICommand OpenBrowserCommand { get; }

    private void RunTheThing()
    {
        // Code for executing the command here.
    }

    private void RunTheThingWithParameter(string parameter)
    {
        // Code for executing the command here.
    }

}

ReactiveCommand is a Reactive Extensions and asynchronous aware implementation of the ICommand interface. It is created using static factory methods which allows you to create command logic that executes either synchronously or asynchronously. The following are the different static factory methods:

  • CreateFromObservable() – Execute the logic using an IObservable.
  • CreateFromTask() – Execute a C# Task Parallel Library (TPL) Task. This allows use also of the C# async/await operators.
  • Create() – Execute a synchronous Func or Action.
  • CreateCombined() – Execute one or more commands.

To create a new command in Avalonia without using any external libraries, you can create a new class that implements the ICommand interface, which requires two methods to be implemented: Execute and CanExecute:

public class RelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Func<object, bool> _canExecute;

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        _execute = execute ?? throw new ArgumentNullException(nameof(execute));
        _canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);

    public void Execute(object parameter) => _execute(parameter);

    public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}

Then use it in your code:

public class HomeModel
{
    public HomeModel()
    {
        OpenBrowserCommand = new RelayCommand(p => { /* some code here */});
    }

    public ICommand OpenBrowserCommand { get; }

    private void RunTheThing()
    {
        // Code for executing the command here.
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading