.NET MAUI Community Toolkit

repository·main·Indexed 25 days ago

https://github.com/communitytoolkit/maui

A collection of common elements and utilities for .NET MAUI development designed to simplify repetitive tasks. The toolkit includes specialized packages for MediaElement, Maps, and Camera, and provides analyzer rules (such as MCT001 and MCT002) to ensure correct initialization and property configuration in MauiProgram.cs.

Tokens
2.6K
Snippets
9
Records
17
Agent score
83%

What's inside .NET MAUI Community Toolkit

  1. Initialize the .NET MAUI Community Toolkit in MauiProgram.cs

    main

    To use the toolkit, you must call the .UseMauiCommunityToolkit() extension method during the app builder initialization in your MauiProgram.cs file.

    using CommunityToolkit.Maui;
    
    public static class MauiProgram
    {
    	public static MauiApp CreateMauiApp()
    	{
    		var builder = MauiApp.CreateBuilder();
    		builder
    			.UseMauiApp<App>()
    			// Initialize the .NET MAUI Community Toolkit by adding the below line of code
    			.UseMauiCommunityToolkit()
    			// After initializing the .NET MAUI Community Toolkit, optionally add additional fonts
    			.ConfigureFonts(fonts =>
    			{
    				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    			});
    
    		// Continue initializing your .NET MAUI App here
    
    		return builder.Build();
    	}
    }
  2. Initialize the .NET MAUI Community Toolkit CameraView

    main

    To use the CameraView component, you must initialize the toolkit in your MauiProgram.cs file by calling the .UseMauiCommunityToolkitCamera() extension method on the MauiAppBuilder.

    using CommunityToolkit.Maui;
    
    public static class MauiProgram
    {
    	public static MauiApp CreateMauiApp()
    	{
    		var builder = MauiApp.CreateBuilder();
    		builder
    			.UseMauiApp<App>()
    			// Initialize the .NET MAUI Community Toolkit CameraView by adding the below line of code
    			.UseMauiCommunityToolkitCamera()
    			.ConfigureFonts(fonts =>
    			{
    				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    			});
    
    		return builder.Build();
    	}
    }
  3. Install Prerelease versions of the .NET MAUI Community Toolkit

    main

    To use the latest pre-release versions, you must first add the GitHub Packages source to your NuGet configuration using a GitHub Personal Access Token (classic) with read:packages scope. Then, install the specific preview versions.

    dotnet nuget add source https://nuget.pkg.github.com/CommunityToolkit/index.json -n CommunityToolkitGitHubNuget -u YOUR_GITHUB_USERNAME -p YOUR_GITHUB_PERSONAL_ACCESS_TOKEN
    
    dotnet add package CommunityToolkit.Maui --version 99.0.0-preview980
    dotnet add package CommunityToolkit.Maui.MediaElement --version 99.0.0-preview980
    dotnet add package CommunityToolkit.Maui.Maps --version 99.0.0-preview980
    dotnet add package CommunityToolkit.Maui.Camera --version 99.0.0-preview980
  4. Install the .NET MAUI Community Toolkit

    main

    Install the stable release versions of the .NET MAUI Community Toolkit and its specialized packages via NuGet using the dotnet CLI.

    dotnet add package CommunityToolkit.Maui
    dotnet add package CommunityToolkit.Maui.MediaElement
    dotnet add package CommunityToolkit.Maui.Maps
    dotnet add package CommunityToolkit.Maui.Camera
  5. Initialize .NET MAUI Community Toolkit Maps

    main

    To use the Maps functionality from the .NET MAUI Community Toolkit, you must call the appropriate initialization extension method in your MauiProgram.cs file.

    • For Windows, use .UseMauiCommunityToolkitMaps("key") and provide your required API key.
    • For all other platforms, use .UseMauiMaps().
    using CommunityToolkit.Maui;
    
    public static class MauiProgram
    {
    	public static MauiApp CreateMauiApp()
    	{
    		var builder = MauiApp.CreateBuilder();
    		builder
    			.UseMauiApp<App>()
    
    #if WINDOWS
    			// Initialize the .NET MAUI Community Toolkit Maps by adding the below line of code
    			.UseMauiCommunityToolkitMaps("key")
    #else
                // For all other platforms
                .UseMauiMaps()
    #endif
    
    			.ConfigureFonts(fonts =>
    			{
    				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    			});
    
    		return builder.Build();
    	}
    }
  6. Initialize the .NET MAUI Community Toolkit

    main

    To use the .NET MAUI Community Toolkit in your application, you must call the .UseMauiCommunityToolkitCore() extension method during the application builder initialization in your MauiProgram.cs file.

    using CommunityToolkit.Maui.Core;
    
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
    
        // Initialise the toolkit
        builder.UseMauiApp<App>().UseMauiCommunityToolkitCore();
    
        // the rest of your logic...
    }
  7. Initialize the .NET MAUI Community Toolkit MediaElement

    main

    To use the MediaElement from the .NET MAUI Community Toolkit, you must call the .UseMauiCommunityToolkitMediaElement() extension method during the app builder initialization in your MauiProgram.cs file.

    You can optionally configure the isAndroidForegroundServiceEnabled parameter. Setting it to false disables the Android foreground service for media playback.

    using CommunityToolkit.Maui;
    
    public static class MauiProgram
    {
    	public static MauiApp CreateMauiApp()
    	{
    		var builder = MauiApp.CreateBuilder();
    		builder
    			.UseMauiApp<App>()
    			// Initialize the .NET MAUI Community Toolkit MediaElement by adding the below line of code
    			.UseMauiCommunityToolkitMediaElement(isAndroidForegroundServiceEnabled: false)
    			.ConfigureFonts(fonts =>
    			{
    				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    			});
    
    		return builder.Build();
    	}
    }