Skip to content

bnotech/extendedwebview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extended WebView for .NET MAUI

A .NET MAUI class library providing an enhanced WebView control with JavaScript interop, navigation events, cookie management, and URL change detection for Android and iOS.

NuGet

Features

  • Execute JavaScript from C# via EvaluateJavaScriptAsync
  • Receive JavaScript calls in C# via invokeCSharpAction(data) bridge
  • Navigation events: Navigating, Navigated, WebViewNavigated
  • URL change detection for SPAs and PWAs via UrlChanged (addresses dotnet/maui#19232)
  • Cookie management: SetCookieAsync, UpdateCookieAsync, RemoveCookieAsync, RemoveAllCookiesAsync
  • Multi-window / popup support on Android
  • Force reload via Refresh()

Supported Platforms

Platform Minimum OS Version
Android 21.0
iOS 12.2

Target Frameworks: net10.0-android, net10.0-ios (MAUI 10.0.41)

Installation

dotnet add package Com.Bnotech.ExtendedWebView

Setup

Register the handler in your MauiProgram.cs:

using Com.Bnotech.ExtendedWebView;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseExtendedWebView();  // <-- Add this

        return builder.Build();
    }
}

Usage

Basic — Load a URL

<!-- In your XAML page -->
<controls:ExtWebView x:Name="webView"
                      Source="https://example.com" />
using Com.Bnotech.ExtendedWebView;

Or set the source in code:

webView.Source = new UrlWebViewSource { Url = "https://example.com" };

Execute JavaScript (C# → JS)

var request = new EvaluateJavaScriptAsyncRequest("document.title");
await webView.EvaluateJavaScriptAsync(request);

Receive JavaScript Calls (JS → C#)

The control automatically injects a invokeCSharpAction(data) function into every page. Subscribe to the event in C#:

webView.JavaScriptAction += (sender, e) =>
{
    string payload = e.Payload;
    // Handle the data sent from JavaScript
};

Then call it from your web page:

invokeCSharpAction("Hello from JavaScript!");

Navigation Events

webView.Navigating += (sender, e) =>
{
    // Fires before navigation starts
};

webView.Navigated += (sender, e) =>
{
    // Fires after navigation completes
};

URL Change Detection (SPA/PWA)

Detects route changes in single-page applications that don't trigger traditional navigation events:

webView.UrlChanged += (sender, e) =>
{
    string newUrl = e.Url;
};

Cookie Management

// Set a cookie
await webView.SetCookieAsync(
    name: "session",
    value: "abc123",
    domain: ".example.com",
    path: "/",
    expires: DateTimeOffset.UtcNow.AddDays(7),
    isSecure: true,
    isHttpOnly: true);

// Update a cookie (overwrites by name/domain/path)
await webView.UpdateCookieAsync(
    name: "session",
    value: "newvalue",
    domain: ".example.com",
    path: "/");

// Remove a specific cookie
await webView.RemoveCookieAsync("session", ".example.com", "/");

// Remove all cookies
await webView.RemoveAllCookiesAsync();

Refresh

Force a reload of the current source:

webView.Refresh();

API Reference

ExtWebView — Properties

Property Type Description
Source WebViewSource The URL or HTML content to display. Default: about:blank

ExtWebView — Methods

Method Description
EvaluateJavaScriptAsync(request) Executes JavaScript in the WebView
SetCookieAsync(...) Sets a cookie in the platform cookie store
UpdateCookieAsync(...) Updates (overwrites) an existing cookie
RemoveCookieAsync(name, domain, path) Removes a specific cookie
RemoveAllCookiesAsync() Removes all cookies
Refresh() Forces a reload of the current source

ExtWebView — Events

Event EventArgs Description
JavaScriptAction JavaScriptActionEventArgs JS called invokeCSharpAction(data)
Navigating WebNavigatingEventArgs Navigation is about to start
Navigated WebNavigatedEventArgs Navigation completed
UrlChanged UrlChangedEventArgs Displayed URL changed (SPA/PWA)
SourceChanged SourceChangedEventArgs Source property was changed

License

See LICENSE.

About

A .NET MAUI WebView with extended JavaScript and Event Handling functionality

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages