66using Avalonia . Platform ;
77using System . Diagnostics . CodeAnalysis ;
88using WebKit ;
9+ using Avalonia . LogicalTree ;
10+ using Avalonia . Controls . Presenters ;
11+ using Avalonia . Metadata ;
912
1013namespace 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+
97136sealed class WKWebViewControlHandle : PlatformHandle , INativeControlHostDestroyableControlHandle
98137{
99138 bool disposedValue ;
0 commit comments