{"record":{"id":"b2290c9bde40290d","repo":"unoplatform/uno","slug":"failed-to-create-an-instance-of-skcanvasvisualbase","errorCode":null,"errorMessage":"Failed to create an instance of SKCanvasVisualBase","messagePattern":"Failed to create an instance of SKCanvasVisualBase","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AddIns/Uno.WinUI.Graphics2DSK/SKCanvasElement.cs","lineNumber":31,"sourceCode":"\n/// <summary>\n/// A <see cref=\"FrameworkElement\"/> that exposes the ability to draw directly on the window using SkiaSharp.\n/// </summary>\n/// <remarks>This is only available on skia-based targets.</remarks>\npublic abstract partial class SKCanvasElement : FrameworkElement\n{\n#if CROSSRUNTIME\n\tprivate SKCanvasVisualBase? _skCanvasVisual;\n\n\tprivate protected override ContainerVisual CreateElementVisual()\n\t{\n\t\tif (ApiExtensibility.CreateInstance<SKCanvasVisualBaseFactory>(this, out var factory))\n\t\t{\n\t\t\treturn _skCanvasVisual = factory.CreateInstance((o, size) => RenderOverride((SKCanvas)o, size), Compositor.GetSharedCompositor());\n\t\t}\n\t\telse\n\t\t{\n\t\t\tthrow new InvalidOperationException($\"Failed to create an instance of {nameof(SKCanvasVisualBase)}\");\n\t\t}\n\t}\n\n\tinternal override bool IsViewHit() => true;\n#endif\n\n\tprotected SKCanvasElement()\n\t{\n\t\tif (!IsSupportedOnCurrentPlatform())\n\t\t{\n\t\t\tthrow new PlatformNotSupportedException($\"This platform does not support {nameof(SKCanvasElement)}. For more information: https://aka.platform.uno/skcanvaselement\");\n\t\t}\n\t}\n\n#if CROSSRUNTIME\n\tpublic static bool IsSupportedOnCurrentPlatform() => ApiExtensibility.IsRegistered<SKCanvasVisualBaseFactory>();\n#else\n\tpublic static bool IsSupportedOnCurrentPlatform() => false;","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.WinUI.Graphics2DSK/SKCanvasElement.cs#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the correct Skia runtime host package (providing SKCanvasVisualBaseFactory) is referenced for the target head.","Before instantiating SKCanvasElement, check SKCanvasElement.IsSupportedOnCurrentPlatform() (which returns ApiExtensibility.IsRegistered<SKCanvasVisualBaseFactory>()) and skip creation when false.","Only use SKCanvasElement on Skia Desktop/Skia-mobile targets where the factory is registered."],"exampleFix":"// before\nvar el = new SKCanvasElement(); // throws on unsupported head\n\n// after\nif (SKCanvasElement.IsSupportedOnCurrentPlatform())\n{\n\tvar el = new SKCanvasElement();\n}","handlingStrategy":"validation","validationCode":"if (!SKCanvasElement.IsSupportedOnCurrentPlatform()) return; // no factory registered\nvar el = new SKCanvasElement();","typeGuard":"static bool CanUseSKCanvasElement() => SKCanvasElement.IsSupportedOnCurrentPlatform();","tryCatchPattern":null,"preventionTips":["Reference the Skia host package that registers SKCanvasVisualBaseFactory in every head that uses SKCanvasElement.","Gate SKCanvasElement instantiation behind IsSupportedOnCurrentPlatform().","Do not reference Uno.WinUI.Graphics2DSK from non-Skia heads."],"tags":["skia","graphics","skcanvaselement","platform","extension"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}