Async Commands (and Async Relay Command) - EASY WPF (.NET CORE)

  Рет қаралды 15,096

SingletonSean

SingletonSean

4 жыл бұрын

Learn how to setup async commands in WPF (or Xamarin). I create an AsyncCommandBase to take care of async specific command requirements so that derived classes simply need to implement their command logic. I also demonstrate various ways to handle exceptions and implement an async RelayCommand.
SOURCE CODE: github.com/SingletonSean/wpf-...

Пікірлер: 51
@shahjahan12003
@shahjahan12003 4 жыл бұрын
Now I clearly understood the async relay commmand. Thanks.
@merwintf
@merwintf 3 жыл бұрын
Man you got everything I ask for :) Keep up the good work man
@hayderdif1886
@hayderdif1886 2 жыл бұрын
Great content, it's helping me a lot during my internship thank you so much, and i was hoping you could do some videos on prism because it's not well covered, again thank you
@Bugaluggit
@Bugaluggit 2 жыл бұрын
Great content and explanations @SingleonSean, these are great content to show young developers I have.
@SingletonSean
@SingletonSean 2 жыл бұрын
Thanks Lucky, glad to hear you're sharing this content with others!
@diontan7096
@diontan7096 4 жыл бұрын
Keep it up your good work and explanation. U deserved more views and subscribers. Thanks mate
@SingletonSean
@SingletonSean 4 жыл бұрын
Thanks Dion, just happy to be helping the subscribers and viewers that I have!
@FuzzyDPozzy
@FuzzyDPozzy 3 жыл бұрын
your videos are the best around man! keep going! Can you make an hour with the basics and best practices for a begginer at wpf? i would be interested in that
@SingletonSean
@SingletonSean 3 жыл бұрын
Hey Fuzzy, I definitely have plans for this. I originally wanted my "Full Stack WPF MVVM" playlist to satisfy this need, but that series has become a bit too long. A quick crash course would be great. Thanks for the suggestion!
@randypenajimenez3893
@randypenajimenez3893 3 жыл бұрын
Really nice content. Keep it up.
@drallstars263
@drallstars263 3 жыл бұрын
Fantastic channel
@abd.alharbi
@abd.alharbi 3 жыл бұрын
Thank you, once again, for a great content. I do have a question about the RelayCommand case, specifically regarding the lambda exception action in the constructor of LoginViewModel. Since the callback is in the view model, is it possible to handle exceptions within the callback and set the StatusMessage there, or are there going to be issues with scoping or race conditions?
@mihaimyh
@mihaimyh 4 жыл бұрын
Hi Sean, I recently discovered your channel and I am thrilled I can still find active desktop developers here. I am planning to watch all your WPF series from the beginning but I have a queestion for you: can you create a video for exception handling in .NET/WPF? Where we should handle the exceptions if we also have class libraries or different layers on our app? At the class library level and inform the user via an event, or we should log the exception and re-throw it?
@SingletonSean
@SingletonSean 4 жыл бұрын
Hey Mihai, that's a great question and exception handling is super important. I'll add that to my list of things to do. Ironically, you commented on a pretty relative video, haha. I typically handle exceptions inside of my commands. This allows me to catch the exception, investigate it, and then set some kind of error message on the view model to notify the view. Of course, it's never a bad idea to catch/re-throw inside of a library for logging purposes, but at the end of the day I like my exceptions to funnel up to my view models/commands so that I can notify the user through the UI. Hopefully that helps a little bit, thanks Mihai!
@jannickbreunis
@jannickbreunis 11 ай бұрын
Hah, SingletonSean. I've been on one of your videos before, but only now with some increase knowledge I appreciate the pun. For this video (although it being 3 years old), I don't think using async void is good in any case. Multiple sources say so, and when I did implement it I landed in the exact situation they warned me for. If you want to fire the task and forget it, make it a void and handle the task appropriate.
@CharwinAmper
@CharwinAmper 4 жыл бұрын
Hi sean, every video that you make are all great and informative! and I am learning a lot. If you don't mind, can you show us also how you would handle a Datagrid with controls like ComboBox in it that's binded to an observablecollection property, I have an idea how to do it but somehow it's having performance issues in the speed of display and how will you handle large records aside from paging. Thanks
@SingletonSean
@SingletonSean 4 жыл бұрын
Hey chaw chaw, sounds like an interesting use case. I'm interested in doing some videos on more specific WPF controls, so this would be a good topic to go over. Also, for performance issues, you might want to check out UI virtualization: docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-controls I've used this in production software and it makes a big difference. It's also easy to setup (just toggle an attached property on the DataGrid, magic) Thanks for the suggestion!
@rahulmathew8713
@rahulmathew8713 3 жыл бұрын
Awesome you gave me the answer to stop spamming button
@anonym1548
@anonym1548 4 жыл бұрын
Hi Sean, very nice videos. Greetings from Russia ;)
@SingletonSean
@SingletonSean 4 жыл бұрын
Greetings Anonym, glad you're enjoying :)
@delysid604
@delysid604 2 жыл бұрын
Great tutorials as always thanks brother. Do you think making CanExecute method virtual to later on override for extra checks is a good idea or is there a better way around it that you can suggest ?
@SingletonSean
@SingletonSean 2 жыл бұрын
Thanks C T! I usually just make CanExecute virtual and override it in base classes. I think this is the simplest and most efficient way.
@netflix_and_zwift
@netflix_and_zwift 3 жыл бұрын
what about cancelling a long running command. cancellationtokens don't really have a reset.. so if you call cts.cancel for example.. if you user fixes the input then presses the go button again the cancellation token still says cancelled. thanks for any feedback or ideas
@ganganu8751
@ganganu8751 6 ай бұрын
Hi Sean, is it a common practice to use the ViewModel in a command to acces the properties of the ViewModel?
@sirinebibicha2024
@sirinebibicha2024 10 ай бұрын
thanks
@vandeljasonstrypper6734
@vandeljasonstrypper6734 2 жыл бұрын
Hi Sean what about properties that use multiple place ? How to set it and use it anywhere
@omostan
@omostan 11 ай бұрын
As always, you're the best. Thank you for sharing your knowledge with us. I have one question though. What if I want to pass a parameter in the callback? Can you please do a video on that? Thanks!
@SingletonSean
@SingletonSean 11 ай бұрын
Thanks omostan! 🚀
@merwintf
@merwintf 3 жыл бұрын
Hey sean us it possible to have an async CanExecute method?
@kuldeepbora3562
@kuldeepbora3562 4 жыл бұрын
Hi Sean, nice videos, good job; Could you please make a video to display data in popup window using mvvm?
@SingletonSean
@SingletonSean 4 жыл бұрын
Thanks Kuldeep! A tutorial on MVVM popups is on the way. Navigation is the most requested topic I've received, and honestly it's probably the one thing in WPF I've run into the most issues with in the past. Definitely a good video topic!
@anaibrahim4361
@anaibrahim4361 3 жыл бұрын
hello thanks for the great tut i tried the same step as you have done with a progress bar but the UI didn't updated can you tell me why ?
@anaibrahim4361
@anaibrahim4361 3 жыл бұрын
i found what i missed sorry your code work perfectly
@semen083
@semen083 4 жыл бұрын
Hi, what you think about realization of AsyncCommand from another youtube blogger "AngelSix"? He use regular RealyCommand, that run a Action. public RelayCommand(Action action) { mAction = action; } And in ViewModel he initialize it in the following way public MainViewModel() { MyCommand = new RelayCommand(async()=> await MyCommandRealization()); }. where MyCommandRealization return Task? Thanks
@SingletonSean
@SingletonSean 4 жыл бұрын
Hey semen, I think there is nothing wrong with this approach. The only thing to consider is exception handling, where in this case the only option is for MyCommandRealization to never throw exceptions. I think that is a valid requirement. The only other issue could be unit testing. If MyCommandRealization is private, then the only way to test the method is to execute the command. With the default RelayCommand, we don't have access to an ExecuteAsync that we can await, so it would be difficult to assert unit test results since MyCommandRealization could still be executing while we are asserting. Of course in a unit test, everything should be nearly synchronous (mocking API/database calls, etc.), so I'm not sure how large of an issue this is.
@veroqtno
@veroqtno 3 жыл бұрын
I read that it's a bad practice to use Async Void, and try/catch it's not gonna help with async void if there is an exception; ref Stephen Cleary. What is you opinion on that statement?
@SingletonSean
@SingletonSean 3 жыл бұрын
Good point, veroqtno. I am familiar with Stephen Cleary's (and other great developer's) async wisdom. However, async void event handlers are acceptable, and a command is similar to an event handler since we're handling button clicks (although it is not syntactically similar, but same concept). We can try and dance around async void as much as we want, but an async void is required for event handlers.
@veroqtno
@veroqtno 3 жыл бұрын
Thank you! Your videos are great please keep up with the great work! I like to see more from WPF MVVM
@WahidRezgui
@WahidRezgui 3 жыл бұрын
great video, as usual . Do you have video where you added CancellactionToken
@SingletonSean
@SingletonSean 3 жыл бұрын
Thanks Wahid! I do not have any videos on CancellationTokens yet, but that is a great suggestion. Perhaps I could show off an AsyncCommand with a CancellationToken as well.
@netflix_and_zwift
@netflix_and_zwift 3 жыл бұрын
@@SingletonSean i didn't see this till i already posted but yes yes yes..if you are ever near MI all the beer or whatever you want is on me
@davidporterrealestate
@davidporterrealestate 4 жыл бұрын
Been watching your videos, Wonder why you don't use UWP?
@SingletonSean
@SingletonSean 4 жыл бұрын
Hey David, I think UWP is great for building Windows apps for the Windows Store and for the platforms that UWP supports. However, since WPF came before UWP, I just haven't checked out UWP yet because I feel WPF is strong enough for desktop development. I think other developers have similar feelings, hence why UWP hasn't gained as much traction yet. I am interested in checking out UWP though, so perhaps in the future! Considering the UI is XAML based, I can't imagine it is difficult to adjust from WPF. I've recently been doing a lot of Xamarin, which is also XAML based, and have noticed many similarities compared to WPF. In fact, it's easier in some cases! Hopefully UWP XAML is similar.
@andrewbeeman9083
@andrewbeeman9083 3 жыл бұрын
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread
@SingletonSean
@SingletonSean 3 жыл бұрын
No worries Andrew, hope this helps :) docs.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.invoke?view=net-5.0
@andrewbeeman9083
@andrewbeeman9083 3 жыл бұрын
@@SingletonSean So I'm going to try and get the results of a request and then update the collection via the result, does that sound right?
@andrewbeeman9083
@andrewbeeman9083 3 жыл бұрын
@@SingletonSean Yup, that worked!
@SingletonSean
@SingletonSean 3 жыл бұрын
That's great news! I've run into that quirk quite a lot with ObservableCollections and ICollectionView.
@leniedor733
@leniedor733 3 жыл бұрын
yea, why dont just zoom sometimes when the explanation is started... man
@SingletonSean
@SingletonSean 3 жыл бұрын
Hey Göthâkar, I've seen some KZbinrs add effects like this in the past, but I didn't really think it added much value. I also have limited experience in video editing. However, this comment inspired me to spend some time with practicing video editing today, and I actually think the zoom is a LOT nicer for visibility (maybe I just didn't like it before because I had never tried, haha). Turns out video editing is pretty fun. Thanks for the suggestion Göthâkar, I'm excited to add this to videos in the future and improve the quality of my videos!
How to Create Layout Components - EASY WPF (.NET CORE)
12:55
SingletonSean
Рет қаралды 9 М.
C# Async Await Mistakes | Part 1
10:19
Amichai Mantinband
Рет қаралды 32 М.
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 11 МЛН
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 36 МЛН
Useful gadget for styling hair 🤩💖 #gadgets #hairstyle
00:20
FLIP FLOP Hacks
Рет қаралды 10 МЛН
Handling WPF Routed Events with MVVM Commands - EASY WPF (.NET Core)
10:54
8 await async mistakes that you SHOULD avoid in .NET
21:13
Nick Chapsas
Рет қаралды 310 М.
Commands In WPF
20:34
DotNetSkoool
Рет қаралды 76 М.
How Async and Await work under the hood in C#
10:40
Hubert Mijalski
Рет қаралды 8 М.
Dependency Injection in .NET Core (.NET 6)
1:00:32
IAmTimCorey
Рет қаралды 186 М.
Какой ноутбук взять для учёбы? #msi #rtx4090 #laptop #юмор #игровой #apple #shorts
0:18
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 12 МЛН
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 8 МЛН