Napacitor C# Runtime API
The Napacitor runtime (@napacitor/maui, assembly name Napacitor) is the C# native runtime that powers Napacitor apps on every MAUI platform. This page covers the classes plugins build on: NapacitorRuntime, NapacitorPluginBase, and NapacitorCall.
This replaces the per-platform bridge APIs of Capacitor (
CapacitorAndroid library /CapacitoriOS library). There is a single C# API surface for all platforms.
Bridge protocol
| Direction | Channel |
|---|---|
| JS → C# | window.DotNet.invokeMethodAsync('Napacitor', 'NAP_invoke', pluginId, method, argsJson) |
| C# → JS (call result) | js.InvokeVoidAsync('NapacitorNative.__emit', callbackId, dataJson) |
| C# → JS (event) | js.InvokeVoidAsync('eval', …) dispatching a CustomEvent |
Call results are envelopes: {"ok":true,"data":…} or {"ok":false,"error":{"code":"…","message":"…"}}.
NapacitorRuntime
The runtime entry point. The generated host starts it from the Blazor root component (Main.razor):
@inject IJSRuntime JS
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await NapacitorRuntime.StartAsync(JS);
}
}
}
Members plugins and host code rely on:
public static NapacitorRuntime? Current { get; } // the running instance
public JsonElement AppConfig { get; } // root of napacitor.config.json
public Task TriggerJSEventAsync(string name, string target = "window", string? dataJson = null)
public Task TriggerJSEventAsync(string name, object? data) // convenience overload
public Task<string?> ReadAssetTextAsync(string url) // read a wwwroot asset by its web URL
AppConfig
Contains the configuration object known to the runtime — the parsed root of napacitor.config.json (a JsonElement; its ValueKind is Undefined until the config has been loaded). This corresponds to Capacitor's bridge.getConfig(). Plugin base classes expose it as AppConfig.
TriggerJSEventAsync
Fires an event on a JavaScript EventTarget such as window or document. If possible, prefer plugin events instead.
// Plugin (base-class method) or host code (via NapacitorRuntime.Current):
await TriggerJSEventAsync("myCustomEvent", "window", """{ "dataKey": "dataValue" }""");
// Convenience overload: object is serialized into the event detail
await TriggerJSEventAsync("myCustomEvent", new { DataKey = "dataValue" });
Note: data must be serializable JSON. JS receives a CustomEvent whose detail is the parsed value (or the raw string if parsing fails):
window.addEventListener('myCustomEvent', (e) => {
console.log((e as CustomEvent).detail);
});
ReadAssetTextAsync(...)
Reads the text content of a wwwroot asset by its web URL. This corresponds to iOS localURL(fromWebURL:) / Android asset reading in Capacitor. It works uniformly on all platforms because the Blazor WebView serves static assets from the same origin — see convertFileSrc in the Web API.
NapacitorPluginBase
Plugins inherit from NapacitorPluginBase and are discovered by reflection — a class marked with [NapacitorPlugin("Name")] needs no manual registration.
[NapacitorPlugin("Battery")]
public class BatteryPlugin : NapacitorPluginBase
{
[NapacitorMethod]
public Task<BatteryInfo> GetBatteryInfo() { … }
protected override void OnListenerAdded(string eventName) { … }
protected override void OnListenerRemoved(string eventName) { … }
}
Members:
protected JsonElement AppConfig { get; } // root config (see above)
protected Task NotifyListeners(string eventName, object data) // push an event to JS listeners
protected virtual void OnListenerAdded(string eventName) // first JS listener arrived
protected virtual void OnListenerRemoved(string eventName) // last JS listener left
protected Task TriggerJSEventAsync(...) // broadcast (see above)
Method parameters: a [NapacitorMethod] takes zero arguments, one strongly typed DTO, or one NapacitorCall (loose access). The bridge deserializes the JS arguments JSON accordingly. See Data Types.
Unavailable functionality should throw NotImplemented(...) — the JS side receives a NapacitorException with code NOT_IMPLEMENTED.
NapacitorCall
A loose-access wrapper around the raw call arguments, for plugins that need ad-hoc access to the JSON payload:
[NapacitorMethod]
public Task<string> Echo(NapacitorCall call)
{
var value = call.Get<string>("value") ?? "fallback";
// … also lets you reach into nested data without a DTO
}
It corresponds to CAPPluginCall / PluginCall in Capacitor, minus the callback bookkeeping: completion is expressed with the method's return value (async Task), and repeated updates are expressed with the event pipeline — see Plugin Events.
Passing data
Notes on how to work with data that is passed between environments can be found here.