Skip to content

Commit 5ab9cfc

Browse files
authored
Revise commandSample in README.
Updated the command implementation details in the README to reflect the use of RelayCommand and removed outdated information.
1 parent e8d3bf2 commit 5ab9cfc

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

src/Avalonia.Samples/MVVM/CommandSample/README.adoc

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -494,44 +494,28 @@ TIP: Learn more about the https://learn.microsoft.com/de-de/dotnet/communitytool
494494

495495
=== Step 2: Create the "Open the POD bay doors" - Command
496496

497-
In the `ViewModel` add a new read-only property of type `ICommand`. We use the interface instead of the specialized class, because it is more flexible. For example You can easily replace it with an async implementation if you need to.
497+
In the `ViewModel`, we use the https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/relaycommand[[`RelayCommand`\]] attribute to create commands.
498+
The https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/relaycommand[[`RelayCommand`\]] attribute instructs the MVVM Toolkit to automatically generate an `IRelayCommand` property that wraps the annotated method.
499+
Even though you don’t write this property yourself, you can bind to it directly from XAML because it’s generated automatically at compile time.
500+
501+
Now we will create a method which will be executed when the `RelayCommand` is invoked.
498502

499503
.CommunityToolkitCommandsViewModel.cs
500504
[source,csharp]
501505
----
502506
/// <summary>
503-
/// This command will ask HAL-9000 to open the pod bay doors
504-
/// </summary>
505-
public ICommand OpenThePodBayDoorsDirectCommand { get; }
506-
----
507-
508-
Now we will create a method which will be executed when the `Command` is invoked:
509-
510-
HAL responds to the human named "Dave" that he can't do that.
511-
This is a famous line from the movie.
512-
513-
.OpenThePodBayDoorsDirectCommand.cs
514-
[source,csharp]
515-
----
516-
// The method that will be executed when the command is invoked
517-
private void OpenThePodBayDoors()
507+
/// The method that will be executed when the RelayCommand is invoked.
508+
/// HAL responds to the human named "Dave" that he can't do that.
509+
/// This is a famous line from the movie.
510+
/// <summary>
511+
[RelayCommand]
512+
private void OpenThePodBayDoorsDirect()
518513
{
519514
ConversationLog.Clear();
520515
AddToConvo( "I'm sorry, Dave, I'm afraid I can't do that.");
521516
}
522517
----
523518

524-
Last but not least we need to initialize our `Command` before we can use it. We will do this in our https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors[`[Constructor\]`] as shown below. We create our Commands using https://www.reactiveui.net/docs/handbook/commands/[`[ReactiveCommand.Create())\]`] method which will take a method or a lambda as parameter.
525-
526-
.OpenThePodBayDoorsDirectCommand.cs
527-
[source,csharp]
528-
----
529-
public ReactiveUiCommandsViewModel()
530-
{
531-
// Init OpenThePodBayDoorsDirectCommand
532-
OpenThePodBayDoorsDirectCommand = new RelayCommand(OpenThePodBayDoors);
533-
}
534-
----
535519

536520
=== Step 3: Create the "Fellow Robot" - Command
537521

0 commit comments

Comments
 (0)