SoftwareDriven.InteropWebView.Client
1.0.5
dotnet add package SoftwareDriven.InteropWebView.Client --version 1.0.5
NuGet\Install-Package SoftwareDriven.InteropWebView.Client -Version 1.0.5
<PackageReference Include="SoftwareDriven.InteropWebView.Client" Version="1.0.5" />
paket add SoftwareDriven.InteropWebView.Client --version 1.0.5
#r "nuget: SoftwareDriven.InteropWebView.Client, 1.0.5"
// Install SoftwareDriven.InteropWebView.Client as a Cake Addin #addin nuget:?package=SoftwareDriven.InteropWebView.Client&version=1.0.5 // Install SoftwareDriven.InteropWebView.Client as a Cake Tool #tool nuget:?package=SoftwareDriven.InteropWebView.Client&version=1.0.5
SoftwareDriven.InteropWebView is a pub/sub hub solution to communicate strongly typed between a MAUI web view that runs a Blazor frontend hosted somewhere else.
- SoftwareDriven.InteropWebView contains shared classes
- SoftwareDriven.InteropWebView.Host is for the MAUI app
- SoftwareDriven.InteropWebView.Client is for the Blazor frontend.
In the MAUI app, register the web view:
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(InteropWebView), typeof(InteropWebViewHandler));
});
And don't forget to use the control in the target page. Bind the 'MessageHub' to it, e.g. from a viewmodel. Two self explanatory methods are provided by the hub:
MessageHub.Subscribe<T>(string topic, Action<T> action);
MessageHub.Publish<T>(string topic, T data);
In the client Blazor page, register the following services (hub and a JS bridge):
services.AddSingleton<InteropMessageHub>();
services.AddSingleton<InteropClient>();
Also add the JS script to your index.html:
<script src="_content/SoftwareDriven.InteropWebView.Client/js/index.bundle.js"></script>
Trigger the initialization of the JS bridge early, e.g. by triggering the creation of the service before 'host.RunAsync' in Program.cs:
var host = builder.Build();
// Trigger the creation of the interop client.
host.Services.GetService<InteropClient>();
await host.RunAsync();
From now on (to be more exact, when 'InteropClient.IsInitialized' is true), you may inject the hub at any place using its publish and subscribe methods to get or receive typed data. The hub and the 'InteropClient' are self-aware of the environment and have the following properties:
InteropClient.IsMauiEnvironment
MessageHub.CanSend
If you need a bidirectional interface, comparable to an API, there is the 'InteropRpc' class. Create only one instance on each side.
rpc = new InteropRpc(messageHub);
// Register handlers for registered methods:
rpc.AddHandler<TResult>("MyRpcFunc", YourTaskFunctionToReturnTResult);
// And call them like that:
await rpc.Call<TResult>("MyRpcFunc", [OptionalParameter]);
The web view provides two events to signal navigation events (before and after page load).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.6)
- SoftwareDriven.InteropWebView (>= 1.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.