diff --git a/docs/mvvm/generators/errors/MVVMTK0039.md b/docs/mvvm/generators/errors/MVVMTK0039.md new file mode 100644 index 000000000..f09fe4de0 --- /dev/null +++ b/docs/mvvm/generators/errors/MVVMTK0039.md @@ -0,0 +1,54 @@ +--- +title: MVVM Toolkit warning MVVMTK0039 +author: Sergio0694 +description: MVVM Toolkit warning MVVMTK0036 +keywords: community toolkit, dotnet, csharp, mvvm, net core, net standard, source generators +dev_langs: + - csharp +--- + +# MVVM Toolkit warning MVVMTK0039 + +Asynchronous methods annotated with `[RelayCommand]` should return a `Task` value, and not be `async void`. This allows the generator to generate asynchronous commands instead (ie. `AsyncRelayCommand` or `AsyncRelayCommand`), which offer additional features such as concurrency control, tracking execution, and more. + +The following sample generates MVVMTK0039: + +```csharp +using System.Threading.Tasks; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace MyApp; + +public partial class SampleViewModel : ObservableObject +{ + [RelayCommand] + private async void DoWorkAsync() + { + await Task.Delay(1000); // Do some work + } +} +``` + +You can see that `DoWorkAsync` is asynchronous, but is just returning `void`. You can fix this by updating the code as follows: + +```csharp +using System.Threading.Tasks; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace MyApp; + +public partial class SampleViewModel : ObservableObject +{ + [RelayCommand] + private async Task DoWorkAsync() + { + await Task.Delay(1000); + } +} +``` + +## Additional resources + +- You can find more examples in the [unit tests](https://github.com/CommunityToolkit/dotnet/tree/main/tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests). diff --git a/docs/mvvm/generators/errors/TOC.yml b/docs/mvvm/generators/errors/TOC.yml index 98050a6d8..9aee67d1a 100644 --- a/docs/mvvm/generators/errors/TOC.yml +++ b/docs/mvvm/generators/errors/TOC.yml @@ -71,6 +71,8 @@ items: href: MVVMTK0035.md - name: MVVMTK0036 href: MVVMTK0036.md +- name: MVVMTK0039 + href: MVVMTK0039.md - name: MVVMTKCFG0001 href: MVVMTKCFG0001.md - name: MVVMTKCFG0002