Skip to content

Commit 88baa44

Browse files
committed
🎨 ios webview2 fixs
1 parent 9b78269 commit 88baa44

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/Avalonia.WebView2.Sample/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public static AppBuilder BuildAvaloniaApp()
6161
.LogToTrace()
6262
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
6363
.With(new X11PlatformOptions())
64-
.AfterSetup(_ => CefRuntimeLoader.Initialize(new CefSettings()
65-
{
66-
RootCachePath = GetCachePath(),
67-
WindowlessRenderingEnabled = false,
68-
LogSeverity = CefLogSeverity.Verbose,
69-
}))
64+
//.AfterSetup(_ => CefRuntimeLoader.Initialize(new CefSettings()
65+
//{
66+
// RootCachePath = GetCachePath(),
67+
// WindowlessRenderingEnabled = false,
68+
// LogSeverity = CefLogSeverity.Verbose,
69+
//}))
7070
#endif
7171
;
7272
return b;

src/Avalonia.WebView2/Platforms/iOS/Controls/WebView2.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
using Avalonia.Platform;
77
using System.Diagnostics.CodeAnalysis;
88
using WebKit;
9+
using Avalonia.LogicalTree;
10+
using Avalonia.Controls.Presenters;
11+
using Avalonia.Metadata;
912

1013
namespace Avalonia.Controls;
1114

12-
partial class WebView2 : global::Avalonia.Controls.NativeControlHost
15+
internal class WKWebViewHandler : global::Avalonia.Controls.NativeControlHost
1316
{
1417
// https://github.com/dotnet/maui/blob/9.0.70/src/Controls/src/Core/HybridWebView/HybridWebView.cs
1518
// https://github.com/dotnet/maui/blob/9.0.70/src/Controls/src/Core/WebView/WebView.cs
@@ -81,10 +84,11 @@ protected virtual WKWebView CreatePlatformView()
8184
return webView;
8285
}
8386

84-
WKWebViewControlHandle? platformHandle;
85-
8687
public WKWebView? WKWebView => platformHandle?.WebView;
8788

89+
90+
WKWebViewControlHandle? platformHandle;
91+
8892
/// <inheritdoc/>
8993
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
9094
{
@@ -94,6 +98,41 @@ protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle paren
9498
}
9599
}
96100

101+
partial class WebView2 : Control
102+
{
103+
[Content]
104+
private Control? Child
105+
{
106+
get => GetValue(ChildProperty);
107+
set => SetValue(ChildProperty, value);
108+
}
109+
110+
private static readonly StyledProperty<Control?> ChildProperty =
111+
AvaloniaProperty.Register<WebView2, Control?>(nameof(Child));
112+
113+
114+
readonly Border _partInnerContainer = new()
115+
{
116+
ClipToBounds = true,
117+
};
118+
119+
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
120+
{
121+
Child = _partInnerContainer;
122+
base.OnAttachedToLogicalTree(e);
123+
wkWebViewHandler = new WKWebViewHandler();
124+
125+
_partInnerContainer.Child = wkWebViewHandler;
126+
127+
wkWebViewHandler.WKWebView.LoadRequest(new NSUrlRequest(new NSUrl("https://baidu.com", false)));
128+
}
129+
130+
WKWebViewHandler? wkWebViewHandler;
131+
132+
133+
public WKWebView? WKWebView => wkWebViewHandler?.WKWebView;
134+
}
135+
97136
sealed class WKWebViewControlHandle : PlatformHandle, INativeControlHostDestroyableControlHandle
98137
{
99138
bool disposedValue;

0 commit comments

Comments
 (0)