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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SoftwareDriven.InteropWebView.Client" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SoftwareDriven.InteropWebView.Client --version 1.0.5                
#r "nuget: SoftwareDriven.InteropWebView.Client, 1.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 153 6/12/2024
1.0.4 146 4/23/2024
1.0.2 126 3/28/2024
1.0.0 119 3/27/2024