Binding MAUI Views to View Models (w/ Dependency Injection!) - MAUI TUTORIALS

2024 ж. 21 Мам.
2 367 Рет қаралды

Learn how to bind views to view models the right way via dependency injection!
🕗 Timestamps:
0:00 - Introduction
0:31 - Issues w/ Binding from XAML or Code-Behind
2:22 - Binding to View Models via Dependency Injection
5:13 - What about Type Safety in XAML?
8:02 - But Now We Have to Write More Code!
9:20 - Summary
📦 Source code: github.com/SingletonSean/maui...
🔁 Full playlist: • MAUI Tutorials
💎 Become a Member: / @singletonsean
🏠 Discord: / discord
🐤 Twitter: / singletonsean_
🏆 Donate: www.paypal.com/donate?hosted_...
✉️ Email: youtube@singletonsean.com
➖➖➖➖➖➖➖➖➖➖
© Credits
🎵 Outro: pixabay.com/music/synthwave-c...

Пікірлер
  • Dude, you need a medal.

    @Jwhite1979@Jwhite19792 ай бұрын
  • I love your videos. Thank you

    @m3xpl4y@m3xpl4y5 ай бұрын
  • Why are you registering this using Singleton vs transient?

    @JoseArce-cz4kl@JoseArce-cz4kl9 сағат бұрын
  • Stil wrapping my head around view model instantiation in WPF vs MAUI. In WPF, you would register a factory in DI and create new instances of a VM using that factory, but in MAUI all the VMs ive seen are singletons. MAUI seems a lot cleaner with much less boilerplate, but definitely a different approach. Makes me feel a bit less in control, which might be a me-problem hahaha

    @Jeremygrmn@Jeremygrmn11 ай бұрын
    • Right! Like even if you register the view model as transient, it acts as a singleton. I suppose this is because the AppShell or something only resolves the view / view model once and then holds on to it? Seems like that should be configurable. I agree, feel like we're a bit less in control hahah

      @SingletonSean@SingletonSean11 ай бұрын
    • @@SingletonSean That is not true. What you observe is that the AppShell instance (and there is only one hold by the very only one instance of App) has only a single instance for each ShellItem, which should not be such a surprise. And it is very easy to proove: Duplicate the MainPage ShellItem and change the Title and Route properties and run the application. Voila, when transient you get different instances and when singleton, well guess what.

      @olivermunzberg1679@olivermunzberg16795 ай бұрын
  • Hi, thanks for the hint. What about the midway, by injecting in the constructor of the code behind. public ProfileView(ProfileViewModel vm) { InitializeComponent(); this.BindingContext = vm; } Or is that not a clean injection?

    @AndiSprick@AndiSprick5 ай бұрын
    • This how I do it. I don’t want logic where I am registering my services. But they both work so you have options.

      @DonovanBrown@DonovanBrown3 ай бұрын
    • I use separate methods for services, views, and view models in the MauiProgram.cs and sometimes will use a separate class altogether if a method gets to big. There is no reason you can't put the extension methods in a separate class instead of having them in your MauiProgram.cs

      @billymartin6497@billymartin64973 ай бұрын
  • Does this mechanism works also for views or just for pages? Because as of now, it does not works when I inject dependency in a view, it will ALWAYS call the default constructor (i.e. parameterless one). Please I'm hardstuck on this

    @leonedev@leonedev4 ай бұрын
  • Could you make a video on how to extract data from the app.config file in .NET MAUI using .NET 8

    @danielfuentes2119@danielfuentes21195 ай бұрын
  • I love this idea and it seems to work well in Shell, but I can't get it to work when using a FlyoutPage. Can you please address this?

    @billymartin6497@billymartin64973 ай бұрын
    • Weird, I will have to look into this!

      @SingletonSean@SingletonSean3 ай бұрын
    • @@SingletonSean Thanks. It may have something to do with the navigation. I am using a FlyoutPage ViewModel with a collection of pages. When tapped, the command sets Detail = new NavigationPage.

      @billymartin6497@billymartin64973 ай бұрын
    • Was wondering if you found anything out. Also, since there is no code for binding in the code behind, how can I call a method in the viewmodel from the code behind? I have a map that has no bindings, so I need to change the location in the code behind. Thanks.

      @billymartin6497@billymartin64973 ай бұрын
    • Why are you even using flyout page when she'll has a flyout option?

      @sipepguru@sipepguru13 күн бұрын
    • @@sipepguru I am using the Shell flyout. I am just using a collectionview inside it.

      @billymartin6497@billymartin649712 күн бұрын
  • ❤❤❤

    @bl1c@bl1c Жыл бұрын
    • 🙌🙌🙌

      @SingletonSean@SingletonSean Жыл бұрын
KZhead