unoplatform/uno · error · InvalidOperationException

Failed to create an instance of SKCanvasVisualBase

Error message

Failed to create an instance of SKCanvasVisualBase

What it means

SKCanvasElement (Uno.WinUI.Graphics2DSK) builds its visual via ApiExtensibility.CreateInstance<SKCanvasVisualBaseFactory>. If no SKCanvasVisualBaseFactory is registered for the current platform/runtime, CreateInstance returns false and the else-branch throws InvalidOperationException. The factory is registered by the Skia runtime host assemblies; its absence means the Graphics2DSK add-in is loaded on a target that has no Skia 2D canvas backend wired up.

Source

Thrown at src/AddIns/Uno.WinUI.Graphics2DSK/SKCanvasElement.cs:31

/// <summary>
/// A <see cref="FrameworkElement"/> that exposes the ability to draw directly on the window using SkiaSharp.
/// </summary>
/// <remarks>This is only available on skia-based targets.</remarks>
public abstract partial class SKCanvasElement : FrameworkElement
{
#if CROSSRUNTIME
	private SKCanvasVisualBase? _skCanvasVisual;

	private protected override ContainerVisual CreateElementVisual()
	{
		if (ApiExtensibility.CreateInstance<SKCanvasVisualBaseFactory>(this, out var factory))
		{
			return _skCanvasVisual = factory.CreateInstance((o, size) => RenderOverride((SKCanvas)o, size), Compositor.GetSharedCompositor());
		}
		else
		{
			throw new InvalidOperationException($"Failed to create an instance of {nameof(SKCanvasVisualBase)}");
		}
	}

	internal override bool IsViewHit() => true;
#endif

	protected SKCanvasElement()
	{
		if (!IsSupportedOnCurrentPlatform())
		{
			throw new PlatformNotSupportedException($"This platform does not support {nameof(SKCanvasElement)}. For more information: https://aka.platform.uno/skcanvaselement");
		}
	}

#if CROSSRUNTIME
	public static bool IsSupportedOnCurrentPlatform() => ApiExtensibility.IsRegistered<SKCanvasVisualBaseFactory>();
#else
	public static bool IsSupportedOnCurrentPlatform() => false;

View on GitHub (pinned to 0418340488)

Solutions

  1. Ensure the correct Skia runtime host package (providing SKCanvasVisualBaseFactory) is referenced for the target head.
  2. Before instantiating SKCanvasElement, check SKCanvasElement.IsSupportedOnCurrentPlatform() (which returns ApiExtensibility.IsRegistered<SKCanvasVisualBaseFactory>()) and skip creation when false.
  3. Only use SKCanvasElement on Skia Desktop/Skia-mobile targets where the factory is registered.

Example fix

// before
var el = new SKCanvasElement(); // throws on unsupported head

// after
if (SKCanvasElement.IsSupportedOnCurrentPlatform())
{
	var el = new SKCanvasElement();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!SKCanvasElement.IsSupportedOnCurrentPlatform()) return; // no factory registered
var el = new SKCanvasElement();

Type guard

static bool CanUseSKCanvasElement() => SKCanvasElement.IsSupportedOnCurrentPlatform();

Prevention

When it happens

Trigger: SKCanvasElement is instantiated on a platform where ApiExtensibility has no SKCanvasVisualBaseFactory registration — e.g. the CROSSRUNTIME code path runs but the Skia host assembly providing the factory was not loaded/registered, or the add-in is referenced from a non-Skia head.

Common situations: Referencing Uno.WinUI.Graphics2DSK from a WASM-DOM or native-mobile head that has no Skia canvas host; missing Skia runtime package reference; referencing the add-in in a unit-test assembly with no host registration.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/b2290c9bde40290d. Report an issue: GitHub.