GithubHelp home page GithubHelp logo

Comments (6)

egvijayanand avatar egvijayanand commented on May 16, 2024 1

A more detailed sample:

dotnet new mauiapp -o NavApp -dp Hierarchical -mvvm

from dotnet-maui-templates.

egvijayanand avatar egvijayanand commented on May 16, 2024

I don't understand your request. Please elaborate.

from dotnet-maui-templates.

sharpwood avatar sharpwood commented on May 16, 2024

Here is an example of using dependency injection in a MAUI app:

First, in the startup class of the MAUI app, we need to create and configure a DI container. We can use the ServiceCollection class provided by the Microsoft.Extensions.DependencyInjection NuGet package:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;

namespace MyMauiApp
{
    public class Startup : IStartup
    {
        public void Configure(IAppHostBuilder appBuilder)
        {
            appBuilder
                .UseMauiApp<MyApp>()
                .ConfigureServices(services =>
                {
                    // Register your services here
                    services.AddTransient<IMyService, MyService>();
                });
        }
    }
}

In the above example, we register the IMyService interface as an instance of the MyService class, so we can use IMyService to retrieve an instance of MyService throughout the application.

Next, we can use the service in other parts of the application. For example, we can inject the IMyService service into the page constructor, as shown below:

public partial class MyPage : ContentPage
{
    private readonly IMyService _myService;

    public MyPage(IMyService myService)
    {
        InitializeComponent();
        _myService = myService;
    }
}

Now, in the code of the MyPage page, we can use the _myService instance to call methods defined in the IMyService interface.

This is just a simple example, but it demonstrates how to use dependency injection in a MAUI app. By using dependency injection, we can decouple different parts of the application and write more easily testable and maintainable code.

from dotnet-maui-templates.

egvijayanand avatar egvijayanand commented on May 16, 2024

You can do that right now using the MauiProgram.cs source file.

Register your dependencies like:

builder.Services.AddTransient<IMyService, MyService>();
// Register the Page with the appropriate scope
builder.Services.AddTransient<MyPage>();

Then inject the dependency into the page as described in your code snippet.

The only catch is that your page has to be resolved from the DI container so that its dependencies are auto-resolved (IMyService is a dependency for MyPage).

For example:

public partial class App : Application
{
  public App(MyPage myPage)
  {
    MainPage = myPage;
  }
}

If you prefer the ConfigureServices approach, it's also available in my MAUI Toolkit here, add the NuGet package as a reference, and then include it in the .NET MAUI startup pipeline.

var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>()
.ConfigureServices(services =>
{
  // Do all the stuff with the "services" parameter
});
return builder.Build();

from dotnet-maui-templates.

egvijayanand avatar egvijayanand commented on May 16, 2024

To get a glimpse of how it works, create a .NET MAUI project from the All-in-One template with the MVVM option.

dotnet new mauiapp -o MvvmApp -mvvm

The ViewModel is injected as a dependency and resolved from the DI container.

from dotnet-maui-templates.

egvijayanand avatar egvijayanand commented on May 16, 2024

Since this is covered in the de facto implementation of .NET MAUI, closing this issue.

from dotnet-maui-templates.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.