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

  Рет қаралды 18,481

SingletonSean

SingletonSean

Күн бұрын

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

Пікірлер: 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 3 жыл бұрын
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 3 жыл бұрын
I just saw we can join your channel
@veroqtno
@veroqtno 3 жыл бұрын
Great, this is what I've been waiting for! Thank you!
@an13selm
@an13selm Жыл бұрын
Excellently stated, with a reference to a challenge many experience within WPF development.
@kairausch1462
@kairausch1462 2 жыл бұрын
insane! Thank you very much!
@bartomiejciurla8472
@bartomiejciurla8472 4 ай бұрын
That was helpfull! Thanks for the tip about disposing
@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
@ivandrofly
@ivandrofly Жыл бұрын
Thanks ;)... another good content!
@SingletonSean
@SingletonSean Жыл бұрын
Thanks Ivandro :)
@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.
@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
@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!
@gabrieldesouza7050
@gabrieldesouza7050 2 жыл бұрын
Heyy one question, using this settup how would yu communicate with a entity framework ? Happy new year !!
@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?
@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??
@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.
@samjiang5851
@samjiang5851 Жыл бұрын
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?
@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?
@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!
@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; }
@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.
@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.
@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.
@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
@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.
@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)
Async Commands (and Async Relay Command) - EASY WPF (.NET CORE)
14:14
Secret Experiment Toothpaste Pt.4 😱 #shorts
00:35
Mr DegrEE
Рет қаралды 38 МЛН
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 7 МЛН
Finger Heart - Fancy Refill (Inside Out Animation)
00:30
FASH
Рет қаралды 29 МЛН
Switching Views - WPF MVVM NAVIGATION TUTORIAL #1
15:28
SingletonSean
Рет қаралды 100 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 118 М.
Creating Reusable Controls - WPF TUTORIALS
17:10
SingletonSean
Рет қаралды 30 М.
How to Create Layout Components - EASY WPF (.NET CORE)
12:55
SingletonSean
Рет қаралды 9 М.
Делегаты и события на практике
9:20
ExtremeCode
Рет қаралды 151 М.
WPF INotifyPropertyChanged and Databinding
14:30
Payload
Рет қаралды 39 М.
What is MVVM (Model-View-ViewModel) Pattern?
10:08
Programming with Mosh
Рет қаралды 326 М.
Какой ноутбук взять для учёбы? #msi #rtx4090 #laptop #юмор #игровой #apple #shorts
0:18
Samsung laughing on iPhone #techbyakram
0:12
Tech by Akram
Рет қаралды 7 МЛН
Лучший браузер!
0:27
Honey Montana
Рет қаралды 1,1 МЛН
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 8 МЛН
Что делать если в телефон попала вода?
0:17
Лена Тропоцел
Рет қаралды 3,3 МЛН
#samsung #retrophone #nostalgia #x100
0:14
mobijunk
Рет қаралды 14 МЛН