Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System;
using Microsoft.MobileBlazorBindings.Core;
using Microsoft.MobileBlazorBindings.WebView.Elements;
using XF = Xamarin.Forms;
Expand All @@ -12,6 +13,8 @@ public class WebViewHandler : ViewHandler
public WebViewExtended Control { get; }

private ulong _onWebMessageReceivedEventHandlerId;
private ulong _onNavigationStartingEventHandlerId;
private ulong _onNavigationFinishedEventHandlerId;

public WebViewHandler(NativeComponentRenderer renderer, WebViewExtended control)
: base(renderer, control)
Expand All @@ -30,6 +33,33 @@ public WebViewHandler(NativeComponentRenderer renderer, WebViewExtended control)
renderer.Dispatcher.InvokeAsync(() => renderer.DispatchEventAsync(_onWebMessageReceivedEventHandlerId, null, new WebView.WebMessageEventArgs { Message = message }));
}
};

ConfigureEvent(
eventName: "onnavigationstarting",
setId: id => _onNavigationStartingEventHandlerId = id,
clearId: id => { if (_onNavigationStartingEventHandlerId == id) { _onNavigationStartingEventHandlerId = 0; } });

Control.OnNavigationStarting += (sender, uri) =>
{
if (_onNavigationStartingEventHandlerId != default)
{
renderer.Dispatcher.InvokeAsync(() => renderer.DispatchEventAsync(_onNavigationStartingEventHandlerId, null, new WebView.NavigationEventArgs() { Uri = uri }));
}
};

ConfigureEvent(
eventName: "onnavigationfinished",
setId: id => _onNavigationFinishedEventHandlerId = id,
clearId: id => { if (_onNavigationFinishedEventHandlerId == id) { _onNavigationFinishedEventHandlerId = 0; } });

Control.OnNavigationFinished += (sender, uri) =>
{
if (_onNavigationFinishedEventHandlerId != default)
{
renderer.Dispatcher.InvokeAsync(() => renderer.DispatchEventAsync(_onNavigationFinishedEventHandlerId, null, new WebView.NavigationEventArgs() { Uri = uri }));
}
};

}

public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
Expand Down
15 changes: 15 additions & 0 deletions src/Microsoft.MobileBlazorBindings/Elements/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static WebView()

[Parameter] public XF.WebViewSource Source { get; set; }
[Parameter] public EventCallback<string> OnWebMessageReceived { get; set; }
[Parameter] public EventCallback<Uri> OnNavigationStarting { get; set; }
[Parameter] public EventCallback<Uri> OnNavigationFinished { get; set; }

public void SendMessage(string message)
{
Expand All @@ -34,6 +36,8 @@ protected override void RenderAttributes(AttributesBuilder builder)
base.RenderAttributes(builder);

builder.AddAttribute("onwebmessagereceived", EventCallback.Factory.Create<WebMessageEventArgs>(this, HandleOnWebMessageReceived));
builder.AddAttribute("onnavigationstarting", EventCallback.Factory.Create<NavigationEventArgs>(this, HandleOnNavigationStarting));
builder.AddAttribute("onnavigationfinished", EventCallback.Factory.Create<NavigationEventArgs>(this, HandleOnNavigationFinished));

switch (Source)
{
Expand All @@ -49,9 +53,20 @@ protected override void RenderAttributes(AttributesBuilder builder)
private Task HandleOnWebMessageReceived(WebMessageEventArgs args)
=> OnWebMessageReceived.InvokeAsync(args.Message);

private Task HandleOnNavigationStarting(NavigationEventArgs args)
=> OnNavigationStarting.InvokeAsync(args.Uri);

private Task HandleOnNavigationFinished(NavigationEventArgs args)
=> OnNavigationFinished.InvokeAsync(args.Uri);

internal class WebMessageEventArgs : EventArgs
{
public string Message { get; set; }
}

internal class NavigationEventArgs : EventArgs
{
public Uri Uri { get; set; }
}
}
}