.NET MAUI
Introduction
This page assumes you have an existing .NET MAUI project. This can be a new or existing project.
Adding Gum NuGet package
The easiest way to add Gum to your project is to use the NuGet package. Open your project in your preferred IDE, or add Gum through the command line.
Add the Gum.SkiaSharp.Maui NuGet package (https://www.nuget.org/packages/Gum.SkiaSharp.Maui)
Modify the csproj:
<PackageReference Include="Gum.SkiaSharp.Maui" />Or add through command line:
dotnet add package Gum.SkiaSharp.MauiAdding Source (Optional)
You can directly link your project to source instead of a NuGet package for improved debuggability, access to fixes and features before NuGet packages are published, or if you are interested in contributing.
To add source, first clone the Gum repository: https://github.com/vchelaru/Gum
If you have already added the Gum NuGet package to your project, remove it.
Add the following projects to your solution:
<Gum Root>/Runtimes/SkiaGum.Maui.csproj
<Gum Root>/SkiaGum/SkiaGum.csproj
<GumRoot>/GumCommon/GumCommon.csproj
Next, add SkiaGum.Maui as a project reference in your game project. Your project might look like this depending on the location of the Gum repository relative to your game project:
<ProjectReference Include="..\Gum\Runtimes\SkiaGum.Maui.csproj" />Initializing Gum and SkiaSharp
Gum in .NET MAUI requires SkiaSharp. If you haven't already set up SkiaSharp for your project, add .UseSkiaSharp() to your Builder. For more information see the SkiaSharp setup: https://learn.microsoft.com/en-us/samples/dotnet/maui-samples/skiasharpmaui-demos/
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseSkiaSharp()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
// This results in 1 pixel in Gum being the same as 1 unit in your app:
SkiaGum.Maui.SkiaGumCanvasView.GlobalScale =
(float)DeviceDisplay.Current.MainDisplayInfo.Density;
return builder.Build();
}
}Adding a ColoredCircle (Testing the Setup)
You can add SkiaGumCanvasView instances to any page or component. SkiaGumCanvasView is a view which inherits from SKCanvasView, but allows adding of Gum runtime elements. To add a SkiaGumCanvasView:
Open the .xaml file for an existing view or component
Add the following namespace to your page or view:\
xmlns:SkiaGum="clr-namespace:SkiaGum.Maui;assembly=SkiaGum.Maui"Add the following to a container, such as to a Grid. Note that the xaml specifies a Name so that code-behind can access the canvas to add objects to it:\
<SkiaGum:SkiaGumCanvasView WidthRequest="200" HeightRequest="200" x:Name="SkiaGumCanvasView"/>Add the following in your C# (code-behind) for the given page or component, such as in the page's constructor:
public MainPage() { InitializeComponent(); // Creates a circle: var circle = new ColoredCircleRuntime(); circle.Color = SKColors.Red; circle.Width = 400; circle.Height = 400; // Adds it so it is drawn SkiaGumCanvasView.AddChild(circle); // Tells the canvas to refresh itself, so the circle appears SkiaGumCanvasView.InvalidateSurface(); }Add the following to your C# (code-behind) to dispose all cached paints when navigating away from the page:\
protected override void OnNavigatedFrom(NavigatedFromEventArgs args) { base.OnNavigatedFrom(args); SkiaGumCanvasView.Dispose(); }
You should now have a red circle on screen.

Last updated
Was this helpful?

