Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeancollas/mauiblazordependencyinjection
Resolver allowing to share dependency injection between Maui and Blazor
https://github.com/jeancollas/mauiblazordependencyinjection
blazor blazor-webassembly dependency-injection maui maui-blazor
Last synced: 3 days ago
JSON representation
Resolver allowing to share dependency injection between Maui and Blazor
- Host: GitHub
- URL: https://github.com/jeancollas/mauiblazordependencyinjection
- Owner: JeanCollas
- Created: 2021-09-26T14:42:53.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-10-02T16:46:26.000Z (over 3 years ago)
- Last Synced: 2025-01-19T17:08:24.259Z (3 days ago)
- Topics: blazor, blazor-webassembly, dependency-injection, maui, maui-blazor
- Language: C#
- Homepage:
- Size: 360 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
## Presentation
This tool aims at simplifying the share of dependency injection between Maui and Blazor in a mixed environment## Nuget
[DependencyInjectionMauiBlazor](https://www.nuget.org/packages/DependencyInjectionMauiBlazor/)
[![NuGet Status](https://img.shields.io/nuget/v/DependencyInjectionMauiBlazor.svg?style=flat)](https://www.nuget.org/packages/DependencyInjectionMauiBlazor/)## Sample
![MAUI Blazor sample](maui-blazor-sample.gif "MAUI Blazor sharing context sample")
## How to use ?
**In MauiProgram class**
```
using MauiBlazorDependencyInjection;public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.RegisterBlazorMauiWebView()
.UseMauiApp()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});builder.Services.AddBlazorWebView();
builder.Services.AddSingleton();// Register any other service / ViewModel / Page here
builder.Services.AddSingleton();// Pre-build the app
var app = builder.Build();
// Intercept and register the ServiceProvider
app.Services.UseResolver();
// Return the app as usual
return app;
}
}
```**a. Use it as your factory from Maui side**
``` csharp
Resolver.ServiceProvider.GetRequiredService();
Console.WriteLine($"Instance number {TestSingletonService.Index}");
```
**b. The services instances will be shared with your Blazor `@inject` services.**
``` razor
@inject TestSingletonService tester
Instance number @(tester.Index).
```