33using Android . Graphics ;
44using Android . Graphics . Drawables ;
55using Android . OS ;
6+ using Android . Runtime ;
67using Android . Views ;
78using Android . Webkit ;
89using AndroidX . Core . View ;
@@ -24,8 +25,6 @@ partial class WebView2
2425 /// </summary>
2526 partial class Handler
2627 {
27- public AWebView ? PlatformView => wv2 . platformHandle ? . WebView ;
28-
2928 protected virtual WebViewClientCompat CreateWebViewClient ( ) => new WebViewClientCompat2 ( this ) ;
3029
3130 protected virtual WebChromeClient CreateWebChromeClient ( ) => new WebChromeClient2 ( this ) ;
@@ -85,25 +84,64 @@ public virtual void DisconnectHandler(AWebView platformView)
8584 }
8685 }
8786
87+ partial class Handler : IPlatformHandle , INativeControlHostDestroyableControlHandle
88+ {
89+ nint IPlatformHandle . Handle => webView == default ? default : webView . Handle ;
90+
91+ string ? IPlatformHandle . HandleDescriptor => "android.view.View" ;
92+
93+ bool disposedValue ;
94+ protected AWebView ? webView ;
95+
96+ protected bool DisposedValue => disposedValue ;
97+
98+ public AWebView ? PlatformView => webView ;
99+
100+ void Dispose ( bool disposing )
101+ {
102+ if ( ! disposedValue )
103+ {
104+ if ( disposing )
105+ {
106+ // 释放托管状态(托管对象)
107+ if ( webView != null )
108+ {
109+ DisconnectHandler ( webView ) ;
110+ webView . Destroy ( ) ; // 销毁平台控件
111+ webView . Dispose ( ) ;
112+ }
113+ }
114+
115+ // 释放未托管的资源(未托管的对象)并重写终结器
116+ // 将大型字段设置为 null
117+ webView = null ;
118+ disposedValue = true ;
119+ }
120+ }
121+
122+ /// <inheritdoc/>
123+ public void Destroy ( )
124+ {
125+ // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
126+ Dispose ( disposing : true ) ;
127+ }
128+ }
129+
88130
89131 public static Context ? GetContext ( IPlatformHandle platformHandle )
90132 {
91133 if ( platformHandle is AndroidViewControlHandle viewControlHandle )
92134 {
93135 return viewControlHandle . View ? . Context ;
94136 }
95- else if ( platformHandle is AndroidWebViewControlHandle webView2ControlHandle )
96- {
97- return webView2ControlHandle . WebView ? . Context ;
98- }
99137 return null ;
100138 }
101139
102140 public AWebView ? AWebView
103141 {
104142 get
105143 {
106- var result = platformHandle ? . WebView ;
144+ var result = viewHandler ? . PlatformView ;
107145 if ( result . IsAlive ( ) )
108146 {
109147 return result ;
@@ -112,8 +150,6 @@ public AWebView? AWebView
112150 }
113151 }
114152
115- AndroidWebViewControlHandle ? platformHandle ;
116-
117153 protected virtual void SetValue ( AWebView webView )
118154 {
119155 if ( _source != null )
@@ -245,51 +281,6 @@ public void LoadDataWithBaseURL(AWebView webView)
245281 }
246282}
247283
248- sealed class AndroidWebViewControlHandle : PlatformHandle , INativeControlHostDestroyableControlHandle
249- {
250- bool disposedValue ;
251- AWebView ? webView ;
252- WebView2 . Handler ? handler ;
253-
254- internal AndroidWebViewControlHandle ( AWebView webView , WebView2 . Handler handler ) : base ( webView . Handle , "android.webkit.WebView" )
255- {
256- this . webView = webView ;
257- this . handler = handler ;
258- }
259-
260- public AWebView ? WebView => webView ;
261-
262- void Dispose ( bool disposing )
263- {
264- if ( ! disposedValue )
265- {
266- if ( disposing )
267- {
268- // 释放托管状态(托管对象)
269- if ( webView != null )
270- {
271- handler ? . DisconnectHandler ( webView ) ;
272- webView . Destroy ( ) ; // 销毁平台控件
273- webView . Dispose ( ) ;
274- }
275- }
276-
277- // 释放未托管的资源(未托管的对象)并重写终结器
278- // 将大型字段设置为 null
279- webView = null ;
280- handler = null ;
281- disposedValue = true ;
282- }
283- }
284-
285- /// <inheritdoc/>
286- public void Destroy ( )
287- {
288- // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
289- Dispose ( disposing : true ) ;
290- }
291- }
292-
293284static class JavaObjectExtensions
294285{
295286 public static bool IsDisposed ( this global ::Java . Lang . Object obj )
0 commit comments