Skip to content

Commit a9a5ed0

Browse files
jamesmcroftSergio0694
authored andcommitted
Added examples for RelayCommand usage for asynchronous actions
1 parent 2348746 commit a9a5ed0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

docs/mvvm/MigratingFromMvvmLight.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,27 @@ using Microsoft.Toolkit.Mvvm.Input;
342342

343343
**Note** on `RelayCommand` constructors. MvvmLight uses weak references to establish the link between the command and the action called from the associated class. This is not required by the MVVM Toolkit implementation and if this optional parameter has been set to `true` in any of your constructors, this will be removed.
344344

345+
### Using RelayCommand with asynchronous actions
346+
347+
If you are currently using the MvvmLight `RelayCommand` implementation with asynchronous actions, the MVVM Toolkit exposes an improved implementation for these scenarios.
348+
349+
You can simply replace your existing `RelayCommand` with the `AsyncRelayCommand` which has been built for asynchronous purposes.
350+
351+
```csharp
352+
// MvvmLight
353+
var command = new RelayCommand(() => this.OnCommandAsync());
354+
var command = new RelayCommand(async () => await this.OnCommandAsync());
355+
356+
// Toolkit.Mvvm
357+
var asyncCommand = new AsyncRelayCommand(this.OnCommandAsync);
358+
```
359+
345360
### RelayCommand Methods
346361

347362
#### RaiseCanExecuteChanged ()
348363

349364
The functionality of `RaiseCanExecuteChanged()` can be achieved with the MVVM Toolkit's `NotifyCanExecuteChanged()` method.
350365

351-
352366
```csharp
353367
// MvvmLight
354368
var command = new RelayCommand(this.OnCommand);
@@ -380,6 +394,20 @@ using Microsoft.Toolkit.Mvvm.Input;
380394

381395
**Note** on `RelayCommand<T>` constructors. MvvmLight uses weak references to establish the link between the command and the action called from the associated class. This is not required by the MVVM Toolkit implementation and if this optional parameter has been set to `true` in any of your constructors, this will be removed.
382396

397+
### Using RelayCommand with asynchronous actions
398+
399+
If you are currently using the MvvmLight `RelayCommand<T>` implementation with asynchronous actions, the MVVM Toolkit exposes an improved implementation for these scenarios.
400+
401+
You can simply replace your existing `RelayCommand<T>` with the `AsyncRelayCommand<T>` which has been built for asynchronous purposes.
402+
403+
```csharp
404+
// MvvmLight
405+
var command = new RelayCommand<string>(async () => await this.OnCommandAsync());
406+
407+
// Toolkit.Mvvm
408+
var asyncCommand = new AsyncRelayCommand<string>(this.OnCommandAsync);
409+
```
410+
383411
### RelayCommand<T> Methods
384412

385413
#### RaiseCanExecuteChanged ()

0 commit comments

Comments
 (0)