This commit is contained in:
Lightczx
2023-09-18 15:38:23 +08:00
parent 7f595a6981
commit 5fa8cc37e8
2 changed files with 40 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ using Snap.Hutao.Service.Notification;
using Snap.Hutao.Service.User;
using Snap.Hutao.ViewModel.User;
using Snap.Hutao.Web.Bridge;
using System.Diagnostics;
namespace Snap.Hutao.View.Control;
@@ -22,7 +23,6 @@ internal partial class WebViewer : UserControl, IRecipient<UserChangedMessage>
[SuppressMessage("", "IDE0052")]
private MiHoYoJSInterface? jsInterface;
private bool isFirstNavigate = true;
public WebViewer()
{
@@ -90,13 +90,12 @@ internal partial class WebViewer : UserControl, IRecipient<UserChangedMessage>
coreWebView2.SetCookie(user.CookieToken, user.LToken, user.SToken).SetMobileUserAgent();
jsInterface = serviceProvider.CreateInstance<MiHoYoJSInterface>(coreWebView2, userAndUid);
if (!isFirstNavigate)
{
coreWebView2.Reload();
}
coreWebView2.Navigate(source);
isFirstNavigate = false;
CoreWebView2Navigator navigator = new(coreWebView2);
await navigator.NavigateAsync("about:blank");
Debug.WriteLine($"Before {source}");
await navigator.NavigateAsync(source);
Debug.WriteLine($"After {WebView.Source}");
}
}
else

View File

@@ -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();
}
}