You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.6 KiB
69 lines
2.6 KiB
using BBWYB.Common.Models;
|
|
using Microsoft.Web.WebView2.Core;
|
|
using Microsoft.Web.WebView2.Wpf;
|
|
using System;
|
|
using io = System.IO;
|
|
|
|
namespace BBWYB.Client
|
|
{
|
|
public class WebView2Manager : IDenpendency
|
|
{
|
|
public WebView2 wb2 { get; private set; }
|
|
//public WebView2Manager()
|
|
//{
|
|
// Init();
|
|
//}
|
|
public void Init(string folderName = "WebView2UserData")
|
|
{
|
|
if (wb2 == null)
|
|
{
|
|
wb2 = new WebView2();
|
|
var wb2Setting = CoreWebView2Environment.CreateAsync(userDataFolder: io.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), folderName)).Result;
|
|
wb2.EnsureCoreWebView2Async(wb2Setting);
|
|
wb2.CoreWebView2InitializationCompleted += Wb2_CoreWebView2InitializationCompleted;
|
|
wb2.NavigationCompleted += Wb2_NavigationCompleted;
|
|
wb2.WebMessageReceived += Wb2_WebMessageReceived;
|
|
}
|
|
}
|
|
|
|
public Action<CoreWebView2WebMessageReceivedEventArgs> OnWebMessageReceived;
|
|
public Action<CoreWebView2NavigationCompletedEventArgs> OnNavigationCompleted;
|
|
public Action<CoreWebView2InitializationCompletedEventArgs> CoreWebView2InitializationCompleted;
|
|
public bool IsInitializationCompleted { get; private set; }
|
|
|
|
private void Wb2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
|
|
{
|
|
OnWebMessageReceived?.Invoke(e);
|
|
}
|
|
|
|
private void Wb2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
|
|
{
|
|
OnNavigationCompleted?.Invoke(e);
|
|
}
|
|
|
|
private void Wb2_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
|
|
{
|
|
CoreWebView2InitializationCompleted?.Invoke(e);
|
|
IsInitializationCompleted = true;
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
if (wb2 != null && wb2.CoreWebView2 != null)
|
|
{
|
|
IsInitializationCompleted = false;
|
|
wb2.CoreWebView2InitializationCompleted -= Wb2_CoreWebView2InitializationCompleted;
|
|
wb2.NavigationCompleted -= Wb2_NavigationCompleted;
|
|
wb2.WebMessageReceived -= Wb2_WebMessageReceived;
|
|
var udf = wb2.CoreWebView2.Environment.UserDataFolder;
|
|
wb2.Dispose();
|
|
wb2 = null;
|
|
try
|
|
{
|
|
io.Directory.Delete(udf, true);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|