Use the CameraView in XAML
mainTo access toolkit components like CameraView within XAML files, declare the following namespace in your XAML root element:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
repository·main·Indexed 25 days ago
https://github.com/communitytoolkit/mauiA 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.
To access toolkit components like CameraView within XAML files, declare the following namespace in your XAML root element:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
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();
}
}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();
}
}To access toolkit features and controls within XAML files, declare the toolkit namespace using the following URI:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"To access toolkit features within your XAML files, declare the following namespace mapping:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"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-preview980Install 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.CameraTo use the Maps functionality from the .NET MAUI Community Toolkit, you must call the appropriate initialization extension method in your MauiProgram.cs file.
.UseMauiCommunityToolkitMaps("key") and provide your required API key..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();
}
}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...
}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();
}
}To use toolkit components like MediaElement within XAML files, declare the following namespace in your XAML root element:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
MCT002 (Severity: Error) ensures that the MaximumRating property is set to a valid value. The value must be between 1 and 10.