Skip to content

Commit 9880162

Browse files
committed
🎨 fixs CookiesProperty
1 parent 6dce173 commit 9880162

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/Avalonia.WebView2/Controls/WebView2.Properties/Cookies.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ namespace Avalonia.Controls;
55

66
partial class WebView2
77
{
8-
/// <summary>
9-
/// The <see cref="AvaloniaProperty" /> which backs the <see cref="Cookies" /> property.
10-
/// </summary>
11-
public static readonly DirectProperty<WebView2, CookieContainer?> CookiesProperty = AvaloniaProperty.RegisterDirect<WebView2, CookieContainer?>(nameof(Cookies), x => x.Cookies);
8+
///// <summary>
9+
///// The <see cref="AvaloniaProperty" /> which backs the <see cref="Cookies" /> property.
10+
///// </summary>
11+
//public static readonly DirectProperty<WebView2, CookieContainer?> CookiesProperty = AvaloniaProperty.RegisterDirect<WebView2, CookieContainer?>(nameof(Cookies), x => x.Cookies);
1212

13-
public CookieContainer Cookies
14-
{
15-
get { return (CookieContainer)GetValue(CookiesProperty); }
16-
set { SetValue(CookiesProperty, value); }
17-
}
13+
public CookieContainer Cookies { get; set; } = null;
1814

1915
readonly HashSet<string> _loadedCookies = new HashSet<string>();
2016

src/Avalonia.WebView2/Platforms/Droid/Controls/WebView2.SetValue.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public void SetSource(IWebView2 webView2, Uri? source)
4242

4343
public void SetZoomFactor(IWebView2 webView2, double zoomFactor)
4444
{
45-
webView2.PlatformWebView?.ZoomBy((float)zoomFactor);
45+
if (zoomFactor >= 0.01d)
46+
{
47+
webView2.PlatformWebView?.ZoomBy((float)zoomFactor);
48+
}
4649
}
4750
}
4851
#endif

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ protected virtual void SetInitValue(AWebView webView)
189189
/// <param name="handler"></param>
190190
public partial class WebViewClientCompat2(Handler handler) : WebViewClientCompat
191191
{
192+
readonly WeakReference<Handler> handler = new(handler);
193+
192194
/// <summary>
193195
/// 获取允许的 URL 协议列表,当非标准协议时,WebView 会显示 net::ERR_UNKNOWN_URL_SCHEME 错误页,设置白名单以屏蔽错误
194196
/// </summary>
@@ -256,6 +258,7 @@ public override bool ShouldOverrideUrlLoading(AWebView? view, string? url)
256258

257259
public override void OnPageStarted(AWebView? view, string? url, Bitmap? favicon)
258260
{
261+
var handler = Handler;
259262
if (!string.IsNullOrWhiteSpace(url))
260263
{
261264
handler.VirtualView.SyncPlatformCookiesToWebView2(url).FireAndForget();
@@ -278,13 +281,27 @@ public override void OnPageStarted(AWebView? view, string? url, Bitmap? favicon)
278281

279282
public override void OnPageFinished(AWebView? view, string? url)
280283
{
284+
var handler = Handler;
281285
if (!string.IsNullOrWhiteSpace(url))
282286
{
283287
handler.VirtualView.SyncPlatformCookiesToWebView2(url).FireAndForget();
284288
}
285289

286290
base.OnPageFinished(view, url);
287291
}
292+
293+
protected Handler? Handler
294+
{
295+
get
296+
{
297+
if (handler.TryGetTarget(out var h))
298+
{
299+
return h;
300+
}
301+
302+
return null;
303+
}
304+
}
288305
}
289306

290307

0 commit comments

Comments
 (0)