In this example, we'll create a simple .NET MAUI common library that contains a shared service for getting a list of colors. This example assumes that you already have Visual Studio and .NET MAUI properly installed and configured.
- Open Visual Studio and create a new
Blank .NET MAUI App project named MauiApp.
- Right-click on the
Solution in the Solution Explorer, and select Add > New Project....
- Choose the
Class Library (.NET Standard) template, and name it MauiApp.Common.
- Click
Create to create the class library project.
- In the
MauiApp.Common project, delete the default Class1.cs file.
- Right-click on the
MauiApp.Common project, select Add > New Item..., then choose Interface and name it IColorService.cs.
Here's a sample implementation for the IColorService interface:
using System.Collections.Generic;
namespace MauiApp.Common
{
public interface IColorService
{
List<string> GetColors();
}
}
- Add a new class called
ColorService.cs in the MauiApp.Common project and implement the IColorService interface:
using System.Collections.Generic;
namespace MauiApp.Common
{
public class ColorService : IColorService
{
public List<string> GetColors()
{
return new List<string> { "Red", "Green", "Blue", "Yellow", "Orange", "Purple", "Brown", "Black", "White", "Gray" };
}
}
}
- Now, go to the
MauiApp project and add a reference to the MauiApp.Common project by right-clicking on Dependencies > Add Project Reference... and selecting the MauiApp.Common project.
- Open the
Startup.cs file in the MauiApp project, and register the ColorService in the ConfigureServices method.
using MauiApp.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
namespace MauiApp
{
public class Startup : IStartup
{
public void Configure(IAppHostBuilder appBuilder)
{
appBuilder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
})
.ConfigureServices((context, services)
=> services.AddSingleton<IColorService, ColorService>()); // Add this line to register the ColorService
}
}
}
- Finally, you can use the
IColorService in your .NET MAUI app. Open the MainPage.xaml.cs file and modify the constructor as shown below:
using System;
using System.Linq;
using MauiApp.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
namespace MauiApp
{
public partial class MainPage : ContentPage
{
public MainPage(IServiceProvider serviceProvider)
{
InitializeComponent();
// Get the IColorService instance from the service provider
var colorService = serviceProvider.GetRequiredService<IColorService>();
// Use the color service to get a list of colors.
var colors = colorService.GetColors();
// Set the page content to a StackLayout holding Label controls for each color.
Content = new StackLayout
{
Children = colors.Select(color => new Label { Text = color, FontSize = 24 }).ToArray()
};
}
}
}
Now, if you run the MauiApp project, it should display a list of colors using the shared IColorService from the MauiApp.Common library.
This example demonstrates a basic .NET MAUI application that uses a common library to share code between your MAUI app and other projects. You can extend the common library with more services, models, and utilities to organize your shared code.