2

I am trying to set up basic DI with the standard Microsoft.Extensions.DependencyInjection NuGet package.

Currently I am registering my dependencies like this:

public App()
{
    InitializeComponent();
    var serviceCollection = new ServiceCollection();
    ConfigureServices(serviceCollection);
}

private static void ConfigureServices(ServiceCollection serviceCollection)
{
    serviceCollection.AddSingleton<IRestClient>(_ => new RestClient("https://localhost:44379/api/"));
    serviceCollection.AddScoped<ICommHubClient, CommHubClient>();
}

I use a viewModel that requires dependencies like this:

 public ChatListViewModel(
        ICommHubClient client,
        IRestClient restClient
        )

In the code behind file of a page (.xaml.cs) I need to supply the viewModel but I need to provide the dependencies there as well.

public ChatListPage()
{
     InitializeComponent();
     BindingContext = _viewModel = new ChatListViewModel(); //CURRENTLY THROWS ERROR BECAUSE NO DEPENDENCIES ARE PASSED!
}

is there anyone who knows how I can apply Dependency Injection (registering and resolving) with Microsoft.Extensions.DependencyInjection in Xamarin Forms?

1 Answer 1

3

You should also register your ViewModels in DI Container, not only your services:

In App.xaml.cs change your code to:

public ServiceProvider ServiceProvider { get; }

public App()
{
    InitializeComponent();
    
    var serviceCollection = new ServiceCollection();
    ConfigureServices(serviceCollection);
    ServiceProvider = serviceCollection.BuildServiceProvider();
    
    MainPage = new ChatListPage();
}

private void ConfigureServices(ServiceCollection serviceCollection)
{
    serviceCollection.AddSingleton<IRestClient>(_ => new RestClient("https://localhost:44379/api/"));
    serviceCollection.AddScoped<ICommHubClient, CommHubClient>();
    serviceCollection.AddTransient<ChatListViewModel>();
}

And then you can resolve your ViewModel from ServiceProvider

public ChatListPage()
{
    InitializeComponent();
    BindingContext = _viewModel = ((App)Application.Current).ServiceProvider.GetService(typeof(ChatListViewModel)) as ChatListViewModel;
}
Sign up to request clarification or add additional context in comments.

1 Comment

add the page as well and put the view model as constructor arg so that the viewmodel is injected as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.