Loading...
Loading...
Guidance for embedding web content in .NET MAUI apps using HybridWebView, including JavaScript–C# interop, bidirectional communication, raw messaging, and trimming/NativeAOT considerations. USE FOR: "HybridWebView", "JavaScript interop", "embed web content", "JS to C# interop", "C# to JavaScript", "web view interop", "raw message", "InvokeJavaScriptAsync", "web content MAUI". DO NOT USE FOR: deep linking from external URLs (use maui-deep-linking), REST API calls (use maui-rest-api), or Blazor Hybrid apps (different from HybridWebView).
npx skill4agent add davidortinau/maui-skills maui-hybridwebviewHybridWebViewHybridRootComponentResources/Raw/wwwroot/
index.html ← entry point (default)
scripts/app.js
styles/app.css<HybridWebView
x:Name="myHybridWebView"
DefaultFile="index.html"
RawMessageReceived="OnRawMessageReceived"
HorizontalOptions="Fill"
VerticalOptions="Fill" />DefaultFileindex.html<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8" /></head>
<body>
<!-- app markup -->
<script src="_hwv/HybridWebView.js"></script>
<script src="scripts/app.js"></script>
</body>
</html>// JS: function addNumbers(a, b) { return a + b; }
var result = await myHybridWebView.InvokeJavaScriptAsync<int>(
"addNumbers",
MyJsonContext.Default.Int32, // return type info
[2, 3], // parameters
[MyJsonContext.Default.Int32, // param 1 type info
MyJsonContext.Default.Int32]); // param 2 type infovar person = await myHybridWebView.InvokeJavaScriptAsync<Person>(
"getPerson",
MyJsonContext.Default.Person,
[id],
[MyJsonContext.Default.Int32]);JsonTypeInfoJsonSerializerContextconst result = await window.HybridWebView.InvokeDotNet('MethodName', [param1, param2]);
window.HybridWebView.InvokeDotNet('LogEvent', ['click', 'button1']); // fire-and-forgetmyHybridWebView.SetInvokeJavaScriptTarget(new MyJsBridge());
public class MyJsBridge
{
public string Greet(string name) => $"Hello, {name}!";
public Person GetPerson(int id) => new Person { Id = id, Name = "Ada" };
}myHybridWebView.SendRawMessage("payload string");window.HybridWebView.SendRawMessage('payload string');void OnRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
{
var message = e.Message;
}window.addEventListener('HybridWebViewMessageReceived', e => {
const message = e.detail.message;
});[JsonSourceGenerationOptions(
WriteIndented = false,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(Person))]
internal partial class MyJsonContext : JsonSerializerContext { }
public class Person
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}Rule: If you add a new type to the interop surface, you must add aattribute to the context.[JsonSerializable(typeof(T))]
InvokeJavaScriptAsynctry
{
var result = await myHybridWebView.InvokeJavaScriptAsync<string>(
"riskyFunction", MyJsonContext.Default.String);
}
catch (Exception ex)
{
Debug.WriteLine($"JS error: {ex.Message}");
}<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>JsonSerializerContextResources/Raw/wwwrootindex.html<script src="_hwv/HybridWebView.js"></script>DefaultFileindex.html[JsonSerializable]JsonSerializerContextSetInvokeJavaScriptTargetInvokeJavaScriptAsync