Тёмный

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

SingletonSean
Подписаться 22 тыс.
Просмотров 15 тыс.
50% 1

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-...

Наука

Опубликовано:

 

17 июл 2020

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 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
@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!
@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
@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!
@randypenajimenez3893
@randypenajimenez3893 3 года назад
Really nice content. Keep it up.
@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!
@jannickbreunis
@jannickbreunis 10 месяцев назад
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.
@drallstars263
@drallstars263 3 года назад
Fantastic channel
@rahulmathew8713
@rahulmathew8713 3 года назад
Awesome you gave me the answer to stop spamming button
@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!
@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!
@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?
@anonym1548
@anonym1548 4 года назад
Hi Sean, very nice videos. Greetings from Russia ;)
@SingletonSean
@SingletonSean 4 года назад
Greetings Anonym, glad you're enjoying :)
@omostan
@omostan 10 месяцев назад
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 10 месяцев назад
Thanks omostan! 🚀
@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
@vandeljasonstrypper6734
@vandeljasonstrypper6734 2 года назад
Hi Sean what about properties that use multiple place ? How to set it and use it anywhere
@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.
@kuldeepbora3562
@kuldeepbora3562 3 года назад
Hi Sean, nice videos, good job; Could you please make a video to display data in popup window using mvvm?
@SingletonSean
@SingletonSean 3 года назад
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!
@sirinebibicha2024
@sirinebibicha2024 9 месяцев назад
thanks
@ganganu8751
@ganganu8751 5 месяцев назад
Hi Sean, is it a common practice to use the ViewModel in a command to acces the properties of the ViewModel?
@merwintf
@merwintf 3 года назад
Hey sean us it possible to have an async CanExecute method?
@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
@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.
@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
@semen083
@semen083 3 года назад
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 3 года назад
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.
@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 RU-vidrs 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!
Далее
C# WPF - Commands [Part 1 Intro + Singular Commands]
17:32
8 await async mistakes that you SHOULD avoid in .NET
21:13
Incredible magic 🤯✨
00:53
Просмотров 5 млн
C# Async Await Mistakes | Part 1
10:19
Просмотров 30 тыс.
Commands In WPF
20:34
Просмотров 76 тыс.
Don't throw exceptions in C#. Do this instead
18:13
Просмотров 251 тыс.
OZON РАЗБИЛИ 3 КОМПЬЮТЕРА
0:57
Просмотров 28 тыс.