You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Avalonia.Samples/MVVM/CommandSample/README.adoc
+11-27Lines changed: 11 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -494,44 +494,28 @@ TIP: Learn more about the https://learn.microsoft.com/de-de/dotnet/communitytool
494
494
495
495
=== Step 2: Create the "Open the POD bay doors" - Command
496
496
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.
498
502
499
503
.CommunityToolkitCommandsViewModel.cs
500
504
[source,csharp]
501
505
----
502
506
/// <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()
518
513
{
519
514
ConversationLog.Clear();
520
515
AddToConvo( "I'm sorry, Dave, I'm afraid I can't do that.");
521
516
}
522
517
----
523
518
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);
0 commit comments