SoftwareDriven.InteropWebView 1.0.4

dotnet add package SoftwareDriven.InteropWebView --version 1.0.4                
NuGet\Install-Package SoftwareDriven.InteropWebView -Version 1.0.4                
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" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SoftwareDriven.InteropWebView --version 1.0.4                
#r "nuget: SoftwareDriven.InteropWebView, 1.0.4"                
#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 as a Cake Addin
#addin nuget:?package=SoftwareDriven.InteropWebView&version=1.0.4

// Install SoftwareDriven.InteropWebView as a Cake Tool
#tool nuget:?package=SoftwareDriven.InteropWebView&version=1.0.4                

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]);
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.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on SoftwareDriven.InteropWebView:

Package Downloads
SoftwareDriven.InteropWebView.Client

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.

SoftwareDriven.InteropWebView.Host

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.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 220 4/23/2024
1.0.2 149 3/28/2024
1.0.0 144 3/27/2024