Build WPF AsyncCommand using C# async await, Task, and MVVM

preview_player
Показать описание
WPF tutorial for building an AsyncCommand class using ICommand interface that provides support for asynchronous commands using the C# async await pattern and Task from the Task Parallel Library when using MVVM in C#.

Project on Azure DevOps/Git:

This class and its interface, IAsyncCommand, will provide the foundation for asynchronous commands in your view model with a clean API and choice to choose from a non-generic version without parameters or generic version with a strongly typed parameter. They allow for proper thread affinity and synchronization as well as proper flow of logic in your view models. You can also data-bind to this command from Xaml and it will work as any other ICommand would, but will stll provide the asynchronous model.

These commands can be executed and awaited directly from your view model or from the view through the command binding system. This implementation hooks into the CommandManager.RequerySuggested system to allow notification of CanExecuteChanged when keyboard and mouse input change, as well as control focus events.

#wpf #mvvm #dotnet
Рекомендации по теме
Комментарии
Автор

Don't forget your community sir, you are a really qualitative channel for a student. It's hard to find some qualitative code to try to code as enterprise. 🤗

ergazia
Автор

This channel and this Video in particular are so ridiculous underrated!
Great content!

TuxCommander
Автор

Found it very handy the implementation details. Thanks for making a video on it.

neeleshbodgal
Автор

This helped me so much, thanks for the video. I got rid of Prism and async void command methods in my application so I can catch exceptions if they occur. Amazing! :-)

milosacimo
Автор

Great video thank you, very helpful, could do you do some day a video about how to avoid memory leaks in MVVM WPF application

nijadmohammed
Автор

why not make the parameter optional ?
Alternatively, why not follow nomenclature -- ie: AsyncCommandWithOption

jacquesdemolay
Автор

Any way or perhaps tip how to cancel long running tasks with this method :)

KJMastalerz
Автор

Thank you for this tutorial and your well-structured code. I do not know if it's me but it seems I can not find your stashed changes on your repo. Could you please advice ?

laurentdoda
Автор

I made a RunCommand:AsyncCommand with public override async Task ExecuteAsync(){ viewModel.Run();} ui still freezing until code finished

creativeideas
welcome to shbcf.ru