Skip to content

Commit abd6cb7

Browse files
committed
🎨 CefGlue Storages
1 parent 2875a82 commit abd6cb7

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
2+
3+
namespace Avalonia.Controls;
4+
5+
partial class WebView2
6+
{
7+
/// <summary>
8+
/// 订阅 WebView2 控件的事件处理程序
9+
/// </summary>
10+
void SubscribeHandlers()
11+
{
12+
this.LoadStart += OnBrowserLoadStart;
13+
}
14+
15+
/// <summary>
16+
/// 取消订阅 WebView2 控件的事件处理程序
17+
/// </summary>
18+
void UnsubscribeHandlers()
19+
{
20+
this.LoadStart -= OnBrowserLoadStart;
21+
}
22+
}
23+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
2+
3+
using Xilium.CefGlue.Common.Events;
4+
5+
namespace Avalonia.Controls;
6+
7+
partial class WebView2
8+
{
9+
void OnBrowserLoadStart(object sender, LoadStartEventArgs e)
10+
{
11+
try
12+
{
13+
var contentLoading = ContentLoading;
14+
if (contentLoading == null)
15+
{
16+
return;
17+
}
18+
contentLoading(this, e);
19+
}
20+
finally
21+
{
22+
var requestUri = e.Frame.Url;
23+
var js = HandlerStorageServiceGenerateJSString(StorageService, requestUri);
24+
if (js != null)
25+
{
26+
EvaluateJavaScript<object>(js);
27+
}
28+
}
29+
}
30+
}
31+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
2+
3+
using Xilium.CefGlue.Common.Events;
4+
5+
namespace Avalonia.Controls;
6+
partial class WebView2
7+
{
8+
/// <summary>
9+
/// DOMContentLoaded is raised when the initial HTML document has been parsed.
10+
/// </summary><remarks>
11+
/// This aligns with the the document's DOMContentLoaded event in HTML.
12+
/// </remarks>
13+
public event EventHandler<LoadStartEventArgs>? ContentLoading;
14+
}
15+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
22

3+
using Avalonia.Interactivity;
4+
35
namespace Avalonia.Controls;
46

57
partial class WebView2 : global::Xilium.CefGlue.Avalonia.AvaloniaCefBrowser
68
{
9+
internal void CefGuleInitialize()
10+
{
11+
SubscribeHandlers();
12+
}
13+
14+
internal void CefGuleDispose()
15+
{
16+
UnsubscribeHandlers();
17+
}
718
}
819
#endif

src/Avalonia.WebView2/Controls/WebView2.Disposable.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ protected virtual void Dispose(bool disposing)
3333
#if !DISABLE_WEBVIEW2_CORE && (WINDOWS || NETFRAMEWORK)
3434
UnsubscribeHandlersAndCloseController();
3535
#endif
36+
37+
38+
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
39+
CefGuleDispose();
40+
#endif
3641
_disposables.ForEach(d => d.Dispose());
3742
}
3843

src/Avalonia.WebView2/Controls/WebView2.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public WebView2()
2525
// 添加控件显示隐藏切时通知 CoreWebView2Controller
2626
_disposables.Add(this.GetPropertyChangedObservable(IsVisibleProperty).AddClassHandler<WebView2>((t, args) => { IsVisibleChanged(args); }));
2727
SetDefaultBackgroundColor(_defaultBackgroundColorDefaultValue);
28+
29+
30+
#if !(WINDOWS || NETFRAMEWORK) && NET8_0_OR_GREATER && !ANDROID && !IOS
31+
CefGuleInitialize();
32+
#endif
2833
}
2934

3035
/// <inheritdoc />

0 commit comments

Comments
 (0)