Тёмный

View Model Communication/Messaging - EASY WPF (.NET 5) 

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

Learn how to send messages and communicate between multiple view models. This concept is crucial for implementing other WPF infrastructure, such as navigation or state management. View model communication involves creating a "pub/sub" mediator object that will take in messages and send them to subscribing view models. Other concepts covered include events and disposing event handlers.
TIMESTAMPS:
0:00 - Introduction
0:21 - Demo Introduction
1:22 - Unrecommended Solution
2:13 - Recommended Solution (Pub/Sub, Mediator)
2:59 - Implementation
6:26 - Testing
6:40 - Disposing Store Subscriptions
8:00 - Conclusion
EVENT MEMORY LEAKS: michaelscodingspot.com/5-tech...
SOURCE CODE: github.com/SingletonSean/wpf-...
OTHER LINKS:
Donations: www.paypal.com/biz/fund?id=UB...

Наука

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

 

2 июл 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 46   
@marcb2519
@marcb2519 3 года назад
your tutorials are actually the best I have found on this platform in terms of style and content, thanks for that
@SingletonSean
@SingletonSean 3 года назад
Thanks Marc, glad the tutorials are helpful!
@pascalguerin5238
@pascalguerin5238 2 года назад
Indeed! If I had your tutorials 10 years ago when I tried WPF the first time, I would've been soooo much more productive and happy! I just donated to you. You should start a patreon or github sponsorship :)
@pascalguerin5238
@pascalguerin5238 2 года назад
I just saw we can join your channel
@an13selm
@an13selm Год назад
Excellently stated, with a reference to a challenge many experience within WPF development.
@bartomiejciurla8472
@bartomiejciurla8472 2 месяца назад
That was helpfull! Thanks for the tip about disposing
@veroqtno
@veroqtno 3 года назад
Great, this is what I've been waiting for! Thank you!
@kairausch1462
@kairausch1462 2 года назад
insane! Thank you very much!
@ElCidPhysics90
@ElCidPhysics90 2 года назад
A diagram showing the connections and communication pathways would be really cool. May need to look at the code and try and come up with one.
@gabrieldesouza7050
@gabrieldesouza7050 2 года назад
Awesomee tutorial bro!! It is Awesomeee
@geocine
@geocine 2 года назад
Thank you for your videos @SingletonSean , I come from a JS frontend background and I am struggling to understand WPF. Most of the videos I see here are already outdated and doesn't click with me. Your videos are very clear, I hope you would be doing more of state management videos and thread/tasks/workers as well.
@SingletonSean
@SingletonSean 2 года назад
That's great to hear geocine! I'm actually a React developer, so a lot of the WPF decisions I make are inspired by experiences with front-end JS frameworks! I'll definitely be covering those topics as I move more into covering the essentials of building WPF apps.
@ivandrofly
@ivandrofly Год назад
Thanks ;)... another good content!
@SingletonSean
@SingletonSean Год назад
Thanks Ivandro :)
@chisoxnation9499
@chisoxnation9499 3 года назад
Great video as usual! What are your thoughts on incorporating the Windows API in 2021 and is it still needed? .NET versions prior and up to 5.0 have wrapped most functions but there are still some that P/Invoke is needed for. Perhaps a video on this?
@hello_world_zz
@hello_world_zz 2 года назад
thanks
@solvedplus858
@solvedplus858 2 года назад
very awesome tutorial many many thanks but why do you pass product class to store and not passing the product viewmodel it self? can we make the productstore as static class??
@piotrgoacki9070
@piotrgoacki9070 Год назад
Thank you for the great content! I have a question: what's wrong with having a main parent view model shared between its children view models? OK it is coupled, but only to the main view model which would act as a shared data source. Isn't it cleaner instead of having custom events for everything? It is not a criticism, but a genuine question. Thank you!
@user-pw3pu8wu3f
@user-pw3pu8wu3f 2 года назад
Question, say that you allow a product to have an image as well which you would select via openfilefdialog and show it in both views. Where you the appropriate place to call fileopenfdialog? In the viewmodel or in the code behind? It's in the code behind how do I communicate from the codebehind to the view model?
@gabrieldesouza7050
@gabrieldesouza7050 2 года назад
Heyy one question, using this settup how would yu communicate with a entity framework ? Happy new year !!
@TheMilliardo
@TheMilliardo 3 года назад
Thank you. Very informative. Stil w8 you're Mvvm navigation tutorial without "main" navigation menu.
@SingletonSean
@SingletonSean 3 года назад
Thanks Adrian! You won't be waiting much longer, stay tuned this week. I'm finishing up editing one navigation video, but I expect this to be a series. It will be fun.
@TheMilliardo
@TheMilliardo 3 года назад
@@SingletonSean Could you show in the video how to add a popup as well? Thank you.
@robertthurman8412
@robertthurman8412 Год назад
Hello Sean, This doesn't seem to work as well as I thought it would as it seems that you have to pass into the constructor viewmodel, an instance of the viewmodel from which you would like to get information. What I've seen is that if I do this then I can't use the viewmodel within my view as it reports that the viewmodel must be parameterless. So am I missing something? or what am I doing wrong?
@AdisonCavani
@AdisonCavani 2 года назад
I have two views (pages) that need to communicate between each other. View1 will fire UpdatedEvent on navigating to View2, and View2 will fire UpdatedEvent on navigating back to View1. Problem: I use DI and ViewModel2 that is connected to View2 is initialized after View1 called UpdatedEvent. So on first navigation, ViewModel2 doesn't fire event, therefore property is not changed. How can I fix this problem?
@samjiang5851
@samjiang5851 11 месяцев назад
Anybody has any idea why we want to separate commands in another folder instead of just putting it in the ViewModel. By doing so we need to add a reference of ViewModel in each command, is it coupling the code?
@lottexy
@lottexy 2 года назад
Great video but I got lost on the ProductListingViewModel... The whole memory leak thing just confuses me. :( other than that great tutorial.
@purswani
@purswani 3 года назад
I have one question! What if we want to edit or delete products. As in this example you displayed two ViewModel in one view. Can you please guide about how to create for views individually for both ViewModels.?
@SingletonSean
@SingletonSean 3 года назад
Good question Purswani! It would definitely be a similar approach. In this demo, the CreateProduct and ProductListing view models were completely separate from each other, so moving each of those to separate views would result in the same functionality. Regardless, I plan to expand on this concept because it's crucial for WPF/front-end applications. I'll definitely get into edit/delete updates when I do my video on managing application state, because I think it's more related to that concept. Unfortunately, that was all a little bit outside the scope of this tutorial since I just wanted to focus on the communication aspect. But stay tuned!
@joyhoon
@joyhoon 2 года назад
How about make ProductStore class as static? As your channel name suggested in Singleton pattern?
@SingletonSean
@SingletonSean 2 года назад
Hey triplehoon, that's a good question. In most cases, it's probably fine to make it static, but I think there are some drawbacks. For example, what if you really wanted multiple instances of a store? This is unlikely in most cases, but I could image how it could happen in larger apps. The other issue with static classes is that they cannot implement interfaces. This causes tight coupling with consumers, and makes testing and mocking difficult. Singletons can implement interfaces, but tight coupling is still an issue if consumer aren't depending on an interface and using constructor injection. Overall, I just prefer to stay away from static classes and singletons. With .NET's dependency injection capabilities, I find it much easier to just inject dependencies through the constructor. It's also nice being able to see what a class depends on just from looking at a constructor, rather than sifting through every method looking for static/singleton class references.
@joyhoon
@joyhoon 2 года назад
@@SingletonSean Wow. Thank you for a detailed answer. It really helps me to understand your concept of communicating between view models.
@GrullonMiguel
@GrullonMiguel 3 года назад
Around 5:27 a message popsup talking about WPF bindings must be on INotifyPropertyChanged objects, otherwise there are potential memory leaks. Can you create a video or point to a video that goes more in-depth? Perhaps start out with code not optimized and then convert to the optimized way to avoid the leaks.
@SingletonSean
@SingletonSean 3 года назад
Good idea Miguel, I will consider expanding on this. In the mean time, here's a good article that summarizes the issue: michaelscodingspot.com/ways-to-cause-memory-leaks-in-dotnet/#:~:text=WPF%20Bindings%20can%20actually%20cause,a%20memory%20leak%20(explanation).
@GrullonMiguel
@GrullonMiguel 3 года назад
@@SingletonSean awesome. Thanks for the link. It is extremely helpful
@zerosandones7547
@zerosandones7547 3 года назад
How do you read these @5:35 (public string Name => _product.Name;) ?
@SingletonSean
@SingletonSean 3 года назад
Good question Zeroes and Ones. That is known as a get accessor property: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties#the-get-accessor
@zerosandones7547
@zerosandones7547 3 года назад
@@SingletonSean so, is this basically a readonly property? I have been using e.g. public string Name { get; }
@MrDragos360
@MrDragos360 3 года назад
WPF is sooo complicated compared to VueJS SPA and both do the same thing. A client/front-end for an API. Any ideea why Microsoft made is so dificult ? Are there any big advantajes of making an app in WPF instead of VueJS plus Electron in case you really need a desktop UI ? It's really weird that it's sooo damn dificult compared to something that can do the same thing.
@SingletonSean
@SingletonSean 3 года назад
Good point Dragos, as someone who builds web apps (usually Vue as well, go Vue!), I feel the same way sometimes. I don't necessarily think Microsoft made it difficult on purpose. Keep in mind WPF was born over a decade ago and quite a while before the birth of reactive JS frameworks. That said, it's kind of surprising how powerful WPF is in terms of reactivity and MVVM. Electron is great. I've used it a few times and it gets the job done. A bit buggy, a tad slower, takes up more space, but great tool. Feels great using JS, and also destroys WPF in terms of building responsive apps. Cross-platform is useful too. So why use WPF? The strongly typed C# is nice, but also contributes to the reason that it feels difficult. It's also a native Windows app, so it's more performant than Electron and allows us to interact with Windows specific APIs easier (registries, file systems, etc.). It's quirky, but I can't see electron ever replacing WPF entirely.
@mikefocal5770
@mikefocal5770 3 года назад
hi, are you using Prism?
@SingletonSean
@SingletonSean 3 года назад
Hey mike, I am not using Prism. I feel comfortable with the raw power of WPF, and do not feel a framework is necessary. Perhaps I'll try one when I feel I need it though!
@mikefocal5770
@mikefocal5770 3 года назад
@@SingletonSean many thanks for your response , you're doing a great tutorials, is there a dedicated video to MVVM, I'm sure you have a lot to say.
@makahmed7130
@makahmed7130 3 года назад
View Models shouldn't directly communicate with each other
@SingletonSean
@SingletonSean 3 года назад
Hey Mak, I agree! That is why we setup the mediator in this video. I'm assuming this comment is just a summary of the video.
@randyblock8747
@randyblock8747 2 года назад
Always good information, but I did like it better when you did things a little slower. A little hard to follow (at least for me)
Далее
ШАР СКВОЗЬ БУТЫЛКУ (СКЕРЕТ)
00:46
▼ЧЁРНАЯ МАГИЯ 🔮
31:15
Просмотров 502 тыс.
C#/Avalonia - Learn MVVM with Avalonia
2:07:00
Просмотров 10 тыс.
C# WPF Tutorial - Multiple Views
20:00
Просмотров 165 тыс.
C#/WPF - Learn MVVM with the .NET Community Toolkit
1:46:13
Creating Reusable Controls - WPF TUTORIALS
17:10
Просмотров 29 тыс.
Развод с OZON - ноутбук за 2875₽
17:48
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33
iPhone перегрелся, что делать?!
1:01