Skip to content

Commit aa09320

Browse files
authored
Expose platform webview for initialization to enable customization (#53)
Fixes #45
1 parent a2a3409 commit aa09320

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

HybridWebView/HybridWebView.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ public partial class HybridWebView : WebView
3535
/// </summary>
3636
public bool EnableWebDevTools { get; set; }
3737

38+
/// <summary>
39+
/// Raised when a raw message is received from the web view. Raw messages are strings that have no additional processing.
40+
/// </summary>
3841
public event EventHandler<HybridWebViewRawMessageReceivedEventArgs>? RawMessageReceived;
3942

4043
/// <summary>
41-
/// Async event handler that is called when a proxy request is received from the webview.
44+
/// Async event handler that is called when a proxy request is received from the web view.
4245
/// </summary>
43-
4446
public event Func<HybridWebViewProxyEventArgs, Task>? ProxyRequestReceived;
4547

48+
/// <summary>
49+
/// Raised after the web view is initialized but before any content has been loaded into the web view. The event arguments provide the instance of the platform-specific web view control.
50+
/// </summary>
51+
public event EventHandler<HybridWebViewInitializedEventArgs>? HybridWebViewInitialized;
52+
53+
4654
public void Navigate(string url)
4755
{
4856
NavigateCore(url);
@@ -54,6 +62,13 @@ protected override async void OnHandlerChanged()
5462

5563
await InitializeHybridWebView();
5664

65+
HybridWebViewInitialized?.Invoke(this, new HybridWebViewInitializedEventArgs()
66+
{
67+
#if ANDROID || IOS || MACCATALYST || WINDOWS
68+
WebView = PlatformWebView,
69+
#endif
70+
});
71+
5772
Navigate(StartPath);
5873
}
5974

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#if WINDOWS
2+
using Microsoft.Web.WebView2.Core;
3+
using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2;
4+
#elif ANDROID
5+
using AWebView = Android.Webkit.WebView;
6+
#elif IOS || MACCATALYST
7+
using WebKit;
8+
#elif TIZEN
9+
using TWebView = Tizen.WebView.WebView;
10+
#endif
11+
12+
namespace HybridWebView
13+
{
14+
/// <summary>
15+
/// Allows configuring the underlying web view after it has been initialized.
16+
/// </summary>
17+
public class HybridWebViewInitializedEventArgs : EventArgs
18+
{
19+
#nullable disable
20+
#if WINDOWS
21+
/// <summary>
22+
/// Gets the <see cref="WebView2Control"/> instance that was initialized.
23+
/// </summary>
24+
public WebView2Control WebView { get; internal set; }
25+
#elif ANDROID
26+
/// <summary>
27+
/// Gets the <see cref="AWebView"/> instance that was initialized.
28+
/// </summary>
29+
public AWebView WebView { get; internal set; }
30+
#elif MACCATALYST || IOS
31+
/// <summary>
32+
/// Gets the <see cref="WKWebView"/> instance that was initialized.
33+
/// the default values to allow further configuring additional options.
34+
/// </summary>
35+
public WKWebView WebView { get; internal set; }
36+
#elif TIZEN
37+
/// <summary>
38+
/// Gets the <see cref="TWebView"/> instance that was initialized.
39+
/// </summary>
40+
public TWebView WebView { get; internal set; }
41+
#endif
42+
}
43+
}

HybridWebView/Platforms/Android/HybridWebView.Android.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ namespace HybridWebView
66
{
77
partial class HybridWebView
88
{
9-
// Using an IP address means that WebView2 doesn't wait for any DNS resolution,
10-
// making it substantially faster. Note that this isn't real HTTP traffic, since
11-
// we intercept all the requests within this origin.
12-
internal static readonly string AppHostAddress = "0.0.0.0";
9+
private static readonly string AppHostAddress = "0.0.0.0";
1310

1411
/// <summary>
1512
/// Gets the application's base URI. Defaults to <c>https://0.0.0.0/</c>
1613
/// </summary>
17-
internal static readonly string AppOrigin = $"https://{AppHostAddress}/";
14+
private static readonly string AppOrigin = $"https://{AppHostAddress}/";
1815

1916
internal static readonly Uri AppOriginUri = new(AppOrigin);
2017

MauiCSharpInteropWebView/MainPage.xaml.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Globalization;
1+
using HybridWebView;
2+
using System.Globalization;
23
using System.IO.Compression;
34
using System.Text;
45

@@ -27,6 +28,16 @@ public MainPage()
2728
myHybridWebView.JSInvokeTarget = new MyJSInvokeTarget(this);
2829

2930
myHybridWebView.ProxyRequestReceived += MyHybridWebView_OnProxyRequestReceived;
31+
32+
myHybridWebView.HybridWebViewInitialized += MyHybridWebView_WebViewInitialized;
33+
}
34+
35+
private void MyHybridWebView_WebViewInitialized(object sender, HybridWebViewInitializedEventArgs e)
36+
{
37+
#if WINDOWS
38+
// Disable the user manually zooming
39+
e.WebView.CoreWebView2.Settings.IsZoomControlEnabled = false;
40+
#endif
3041
}
3142

3243
public string CurrentPageName => $"Current hybrid page: {_currentPage}";

0 commit comments

Comments
 (0)