Commands and Events - Refactoring WPF Window Code for MVVM

Поділитися
Вставка
  • Опубліковано 6 вер 2024
  • For me, MVVM makes a lot of sense with binding until I have to work with window controls.
    Then it all falls apart.
    Things seem to get weird when we want to have actions that we're hooking up to. How do we show a window? How do we close one? How do we know when the window itself is closing?
    These are all questions that I'll answer in this video. Let's see how to make it happen with commands and events!
    ----
    🔑 Membership & Subscriptions:
    - 📨 Weekly Newsletter: weekly.devlead...
    - 🏘️ Private Discord Community: / discord
    - 📽️ UA-cam Membership: / @devleader
    🧠 Courses:
    - All Courses: www.devleader....
    - Getting Started with C#: dometrain.com/...
    - Deep dive C#: dometrain.com/...
    - C# Zero to Hero BUNDLE: dometrain.com/...
    - Refactoring For C# Devs: dometrain.com/...
    - [FREE] Intro to Software Development: • [FREE MINI COURSE] - I...
    🗣️ Social Media & Links:
    - All My Links: linktr.ee/devl...
    - Blog: www.devleader.ca/
    - TikTok: / devleader
    - LinkedIn: / nickcosentino
    - Threads: threads.net/@d...
    - Twitter: / devleaderca
    - Facebook: / devleaderca
    - Instagram: / dev.leader
    - GitHub: github.com/nco...
    - Twitch: / devleaderca
    - UA-cam: / @devleader
    ❤️ Affiliations & Products/Services That I Love:
    - VPS hosting from RackNerd: my.racknerd.co...
    - VPS hosting from Contabo: www.jdoqocy.co...
    - My newsletter platform ConverKit: convertkit.com...
    - My newsletter referral system SparkLoop: dash.sparkloop...
    - My AI shorts helper Opus Clip: opus.pro/?via=...
    - I try to help answer questions at Quora: www.quora.com/...
    - My favorite computer parts store Newegg: click.linksyne...
    - My favorite supplement store Bulk Supplements: glnk.io/63qn/d...
    ----
    #dotnet #csharp #wpf #frontend

КОМЕНТАРІ • 10

  • @DevLeader
    @DevLeader  Місяць тому

    💡 Learn how to program in C#:
    - dometrain.com/course/getting-started-csharp?affcode=1115529_nl-teyzg
    🧠Deep dive on C#:
    - dometrain.com/course/deep-dive-csharp?affcode=1115529_nl-teyzg
    🎁Zero to Hero C# Bundle:
    - dometrain.com/bundle/from-zero-to-hero-csharp/?affcode=1115529_nl-teyzg
    💪 Skill up your refactoring:
    - dometrain.com/course/from-zero-to-hero-refactoring-for-csharp-developers?affcode=1115529_nl-teyzg
    ✉ Subscribe to my free software engineering newsletter:
    - subscribe.devleader.ca

  • @HarleyPebley
    @HarleyPebley 19 днів тому +1

    You mentioned multiple levels of view models. That kind of feels like what's almost happening with the Presenter and VM where the Presenter is the WPF agnostic level and the VM is the WPF knowledgable one. The exception to this is the Presenter is executing the command which gives it a WPF dependency. I think of Command execution is how the V invokes an action in the VM. It feels "off" for that to be the way the Presenter invokes the action. Personally, I'd have e.g. a Close method on the VM interface and just not expose the commands there. Both the Presenter and Command would call that Close method, which in turn would fire the Close event behind the scenes like the command implementation does now. (Hope that all makes sense.)

    • @DevLeader
      @DevLeader  19 днів тому

      @@HarleyPebley if you want to do any binding in the XAML, you're working with commands unfortunately. But you're essentially saying close to what I end up doing in my own projects.
      If you *don't* need binding on these things, ditch the command. I find it completely useless otherwise, and you get a dependency on WPF stuff when you really don't need to. In that case, it's exactly as you said 🙂
      If you DO need to bind *and* you're keen on having that level of separation, I create view models that WPF understands (with commands) that wrap a non-wpf view model (methods and events) and that way my presenter never needs to know about anything WPF at all.
      I may not have explained it clearly in the video, and this comment probably isn't much better 😂

  • @williamliu8985
    @williamliu8985 Місяць тому

    8:04 Whenever I use event hooks, I am always worried about memory leaks. Could you share how you use events safely? It would be best if you have some examples to demonstrate what leaks might occur and how to prove that a memory leak has occurred?

    • @krccmsitp2884
      @krccmsitp2884 Місяць тому +1

      Make sure to unsubscribe from events before the object is being freed, e.g. by implementing IDisposable. With WPF controls, you also could use its Unloaded event instead.

    • @williamliu8985
      @williamliu8985 Місяць тому

      @@krccmsitp2884 Thanks! Does it mean do the event hook in load event, and unhook it in unload event? Any idea about how to verify there is no memory leak?

    • @DevLeader
      @DevLeader  Місяць тому

      ​@@williamliu8985most times I'm hooking things up they live for the same lifetime or in fact the entire lifetime of the app. In this case... No concern 🙂
      But yeah, you need an opposite spot for unhooking things. IDisposable is great, but you need to know when to dispose of it still.
      If I'm populating a list control and I need to subscribe to events on items, I'll make sure when I'm clearing I'm unhooking things.
      You can probably leverage the performance analysis tools in visual studio to check -- but I'm not sure what tier is needed

    • @williamliu8985
      @williamliu8985 Місяць тому

      @@DevLeader Thanks for the suggestion! I had no good idea to check if there was a leak. This made me always be careful when using events.