Skip to content

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

PlatformMinimum OS Version
Android21.0
iOS12.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:

usingCom.Bnotech.ExtendedWebView;publicstaticclassMauiProgram{publicstaticMauiAppCreateMauiApp(){varbuilder=MauiApp.CreateBuilder();builder.UseMauiApp<App>().UseExtendedWebView();// <-- Add thisreturnbuilder.Build();}}

Usage

Basic — Load a URL

<!-- In your XAML page -->
<controls:ExtWebViewx:Name="webView"Source="https://example.com" />
usingCom.Bnotech.ExtendedWebView;

Or set the source in code:

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

Execute JavaScript (C# → JS)

varrequest=newEvaluateJavaScriptAsyncRequest("document.title");awaitwebView.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)=>{stringpayload=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)=>{stringnewUrl=e.Url;};

Cookie Management

// Set a cookieawaitwebView.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)awaitwebView.UpdateCookieAsync(name:"session",value:"newvalue",domain:".example.com",path:"/");// Remove a specific cookieawaitwebView.RemoveCookieAsync("session",".example.com","/");// Remove all cookiesawaitwebView.RemoveAllCookiesAsync();

Refresh

Force a reload of the current source:

webView.Refresh();

API Reference

ExtWebView — Properties

PropertyTypeDescription
SourceWebViewSourceThe URL or HTML content to display. Default: about:blank

ExtWebView — Methods

MethodDescription
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

EventEventArgsDescription
JavaScriptActionJavaScriptActionEventArgsJS called invokeCSharpAction(data)
NavigatingWebNavigatingEventArgsNavigation is about to start
NavigatedWebNavigatedEventArgsNavigation completed
UrlChangedUrlChangedEventArgsDisplayed URL changed (SPA/PWA)
SourceChangedSourceChangedEventArgsSource property was changed

License

See LICENSE.

About

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

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages