c# visualstudio Maui common library example
- User
c# visualstudio Maui common library example
- Assistant
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 Appproject namedMauiApp. - Right-click on the
Solutionin the Solution Explorer, and selectAdd>New Project.... - Choose the
Class Library (.NET Standard)template, and name itMauiApp.Common. - Click
Createto create the class library project. - In the
MauiApp.Commonproject, delete the defaultClass1.csfile. - Right-click on the
MauiApp.Commonproject, selectAdd>New Item..., then chooseInterfaceand name itIColorService.cs.
Here's a sample implementation for the
IColorServiceinterface:using System.Collections.Generic; namespace MauiApp.Common { public interface IColorService { List<string> GetColors(); } }- Add a new class called
ColorService.csin theMauiApp.Commonproject 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
MauiAppproject and add a reference to theMauiApp.Commonproject by right-clicking onDependencies>Add Project Reference...and selecting theMauiApp.Commonproject. - Open the
Startup.csfile in theMauiAppproject, and register theColorServicein theConfigureServicesmethod.
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
IColorServicein your .NET MAUI app. Open theMainPage.xaml.csfile 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
MauiAppproject, it should display a list of colors using the sharedIColorServicefrom theMauiApp.Commonlibrary.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.
- Open Visual Studio and create a new
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.