Skip to content

Commit

Permalink
fix #880
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Sep 18, 2023
1 parent 7f595a6 commit 5fa8cc3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Snap.Hutao.Service.User;
using Snap.Hutao.ViewModel.User;
using Snap.Hutao.Web.Bridge;
using System.Diagnostics;

namespace Snap.Hutao.View.Control;

Expand All @@ -22,7 +23,6 @@ internal partial class WebViewer : UserControl, IRecipient<UserChangedMessage>

[SuppressMessage("", "IDE0052")]
private MiHoYoJSInterface? jsInterface;
private bool isFirstNavigate = true;

public WebViewer()
{
Expand Down Expand Up @@ -90,13 +90,12 @@ private async void RefreshWebview2Content()

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
Expand Down
34 changes: 34 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Web/Bridge/CoreWebView2Navigator.cs
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();
}
}

0 comments on commit 5fa8cc3

Please sign in to comment.