-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Snap.Hutao/Snap.Hutao/Web/Bridge/CoreWebView2Navigator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Microsoft.Web.WebView2.Core; | ||
using Windows.Foundation; | ||
|
||
namespace Snap.Hutao.Web.Bridge; | ||
|
||
internal sealed class CoreWebView2Navigator | ||
{ | ||
private readonly TypedEventHandler<CoreWebView2, CoreWebView2NavigationCompletedEventArgs> coreWebView2NavigationCompletedEventHandler; | ||
private readonly CoreWebView2 coreWebView2; | ||
private TaskCompletionSource navigationTask = new(); | ||
|
||
public CoreWebView2Navigator(CoreWebView2 coreWebView2) | ||
{ | ||
coreWebView2NavigationCompletedEventHandler = OnWebviewNavigationCompleted; | ||
this.coreWebView2 = coreWebView2; | ||
} | ||
|
||
public async ValueTask NavigateAsync(string url) | ||
{ | ||
coreWebView2.NavigationCompleted += coreWebView2NavigationCompletedEventHandler; | ||
coreWebView2.Navigate(url); | ||
await navigationTask.Task.ConfigureAwait(false); | ||
coreWebView2.NavigationCompleted -= coreWebView2NavigationCompletedEventHandler; | ||
} | ||
|
||
private void OnWebviewNavigationCompleted(CoreWebView2 webView2, CoreWebView2NavigationCompletedEventArgs args) | ||
{ | ||
navigationTask.TrySetResult(); | ||
navigationTask = new(); | ||
} | ||
} |