2022-07-06 (水)
[WPF] WebView2 のイベントをデバッグで確認してみる
WebView2 型と CoreWebView2 型のイベントがいつ発生するのか、その内容をデバッグで確認したいと思います。
環境
- Microsoft.Web.WebView2 1.0.1264.42
- WebView2 103.0.1264.44
- .NET 6.0.301
- C# 10.0
- Visual Studio 2022 Version 17.2.5
- Windows 10 Pro 64bit 21H1 19043.1766
実装
これらのイベントを順番に購読してみます。
- WebView2 https://docs.microsoft.com/ja-jp/dotnet/api/microsoft.web.webview2.wpf.webview2#events
- CoreWebView2 https://docs.microsoft.com/ja-jp/dotnet/api/microsoft.web.webview2.core.corewebview2?view=webview2-dotnet-1.0.1264.42#events
⚠️ 注意: イベントから渡ってきた EventArgs の全てのプロパティをデバッグ出力しているわけではありません。
必要に応じてブレイクポイントを挿入するなり、デバッグしてください。
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Web.WebView2.Core;
namespace WebView2Study;
internal partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
InitAsync();
}
private async void InitAsync()
{
await WebView.EnsureCoreWebView2Async();
// WebView2 Runtime Version
Debug.Print($"BrowserVersionString = {WebView.CoreWebView2.Environment.BrowserVersionString}");
// WebView
WebView.ContentLoading += WebViewOnContentLoading;
WebView.CoreWebView2InitializationCompleted += WebViewOnCoreWebView2InitializationCompleted;
WebView.NavigationCompleted += WebViewOnNavigationCompleted;
WebView.NavigationStarting += WebViewOnNavigationStarting;
WebView.SourceChanged += WebViewOnSourceChanged;
WebView.WebMessageReceived += WebViewOnWebMessageReceived;
WebView.ZoomFactorChanged += WebViewOnZoomFactorChanged;
// WebView.CoreWebView2
WebView.CoreWebView2.BasicAuthenticationRequested += CoreWebView2OnBasicAuthenticationRequested;
WebView.CoreWebView2.ClientCertificateRequested += CoreWebView2OnClientCertificateRequested;
WebView.CoreWebView2.ContainsFullScreenElementChanged += CoreWebView2OnContainsFullScreenElementChanged;
WebView.CoreWebView2.ContentLoading += CoreWebView2OnContentLoading;
WebView.CoreWebView2.ContextMenuRequested += CoreWebView2OnContextMenuRequested;
WebView.CoreWebView2.DocumentTitleChanged += CoreWebView2OnDocumentTitleChanged;
WebView.CoreWebView2.DOMContentLoaded += CoreWebView2OnDOMContentLoaded;
WebView.CoreWebView2.DownloadStarting += CoreWebView2OnDownloadStarting;
WebView.CoreWebView2.FrameCreated += CoreWebView2OnFrameCreated;
WebView.CoreWebView2.FrameNavigationCompleted += CoreWebView2OnFrameNavigationCompleted;
WebView.CoreWebView2.FrameNavigationStarting += CoreWebView2OnFrameNavigationStarting;
WebView.CoreWebView2.HistoryChanged += CoreWebView2OnHistoryChanged;
WebView.CoreWebView2.IsDefaultDownloadDialogOpenChanged += CoreWebView2OnIsDefaultDownloadDialogOpenChanged;
WebView.CoreWebView2.IsDocumentPlayingAudioChanged += CoreWebView2OnIsDocumentPlayingAudioChanged;
WebView.CoreWebView2.IsMutedChanged += CoreWebView2OnIsMutedChanged;
WebView.CoreWebView2.NavigationCompleted += CoreWebView2OnNavigationCompleted;
WebView.CoreWebView2.NavigationStarting += CoreWebView2OnNavigationStarting;
WebView.CoreWebView2.NewWindowRequested += CoreWebView2OnNewWindowRequested;
WebView.CoreWebView2.PermissionRequested += CoreWebView2OnPermissionRequested;
WebView.CoreWebView2.ProcessFailed += CoreWebView2OnProcessFailed;
WebView.CoreWebView2.ScriptDialogOpening += CoreWebView2OnScriptDialogOpening;
WebView.CoreWebView2.ServerCertificateErrorDetected += CoreWebView2OnServerCertificateErrorDetected;
WebView.CoreWebView2.SourceChanged += CoreWebView2OnSourceChanged;
WebView.CoreWebView2.StatusBarTextChanged += CoreWebView2OnStatusBarTextChanged;
WebView.CoreWebView2.WebMessageReceived += CoreWebView2OnWebMessageReceived;
WebView.CoreWebView2.WebResourceRequested += CoreWebView2OnWebResourceRequested;
WebView.CoreWebView2.WebResourceResponseReceived += CoreWebView2OnWebResourceResponseReceived;
WebView.CoreWebView2.WindowCloseRequested += CoreWebView2OnWindowCloseRequested;
WebView.Source = new Uri("https://developer.microsoft.com/microsoft-edge/webview2/");
}
// WebView2
private void WebViewOnContentLoading(object? sender, CoreWebView2ContentLoadingEventArgs e)
{
Debug.Print($"WebView.ContentLoading: {nameof(e.IsErrorPage)} = {e.IsErrorPage}, {nameof(e.NavigationId)} = {e.NavigationId}");
}
private void WebViewOnCoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2InitializationCompleted: {nameof(e.IsSuccess)} = {e.IsSuccess}, {nameof(e.InitializationException)} = {e.InitializationException}");
}
private void WebViewOnNavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
Debug.Print($"WebView.NavigationCompleted: {nameof(e.NavigationId)} = {e.NavigationId}, {nameof(e.IsSuccess)} = {e.IsSuccess}, {nameof(e.HttpStatusCode)} = {e.HttpStatusCode}, {nameof(e.WebErrorStatus)} = {e.WebErrorStatus}");
}
private void WebViewOnNavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
{
Debug.Print($"WebView.NavigationStarting: {nameof(e.NavigationId)} = {e.NavigationId}, {e.Uri}");
}
private void WebViewOnSourceChanged(object? sender, CoreWebView2SourceChangedEventArgs e)
{
Debug.Print($"WebView.SourceChanged: {nameof(e.IsNewDocument)} = {e.IsNewDocument}");
}
private void WebViewOnWebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
{
Debug.Print($"WebView.WebMessageReceived: {nameof(e.Source)} = {e.WebMessageAsJson}");
}
private void WebViewOnZoomFactorChanged(object? sender, EventArgs e)
{
Debug.Print("WebView.ZoomFactorChanged:");
}
// WebView.CoreWebView2
private void CoreWebView2OnBasicAuthenticationRequested(object? sender, CoreWebView2BasicAuthenticationRequestedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.BasicAuthenticationRequested: {nameof(e.Uri)} = {e.Uri}");
}
private void CoreWebView2OnClientCertificateRequested(object? sender, CoreWebView2ClientCertificateRequestedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.ClientCertificateRequested: {nameof(e.Host)} = {e.Host}");
}
private void CoreWebView2OnContainsFullScreenElementChanged(object? sender, object e)
{
Debug.Print($"WebView.CoreWebView2.ContainsFullScreenElementChanged: {e}");
}
private void CoreWebView2OnContentLoading(object? sender, CoreWebView2ContentLoadingEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.ContentLoading {nameof(e.IsErrorPage)} = {e.IsErrorPage}, {nameof(e.NavigationId)} = {e.NavigationId}");
}
private void CoreWebView2OnContextMenuRequested(object? sender, CoreWebView2ContextMenuRequestedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.ContextMenuRequested: {nameof(e.ContextMenuTarget)} = {e.ContextMenuTarget.SelectionText}");
}
private void CoreWebView2OnDocumentTitleChanged(object? sender, object e)
{
Debug.Print($"WebView.CoreWebView2.DocumentTitleChanged: {(sender as CoreWebView2)?.DocumentTitle}");
}
private void CoreWebView2OnDOMContentLoaded(object? sender, CoreWebView2DOMContentLoadedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.DOMContentLoaded: {nameof(e.NavigationId)} = {e.NavigationId}");
}
private void CoreWebView2OnDownloadStarting(object? sender, CoreWebView2DownloadStartingEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.DownloadStarting: {nameof(e.ResultFilePath)} = {e.ResultFilePath}");
}
private void CoreWebView2OnFrameCreated(object? sender, CoreWebView2FrameCreatedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.FrameCreated: {nameof(e.Frame)} = {e.Frame.Name}");
}
private void CoreWebView2OnFrameNavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.FrameNavigationCompleted: {nameof(e.NavigationId)} = {e.NavigationId}, {nameof(e.IsSuccess)} = {e.IsSuccess}, {nameof(e.HttpStatusCode)} = {e.HttpStatusCode}, {nameof(e.WebErrorStatus)} = {e.WebErrorStatus}");
}
private void CoreWebView2OnFrameNavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.FrameNavigationStarting: {nameof(e.NavigationId)} = {e.NavigationId}, {nameof(e.Uri)} = {e.Uri}");
}
private void CoreWebView2OnHistoryChanged(object? sender, object e)
{
Debug.Print("WebView.CoreWebView2.HistoryChanged:");
}
private void CoreWebView2OnIsDefaultDownloadDialogOpenChanged(object? sender, object e)
{
Debug.Print("WebView.CoreWebView2.IsDefaultDownloadDialogOpenChanged:");
}
private void CoreWebView2OnIsDocumentPlayingAudioChanged(object? sender, object e)
{
Debug.Print("WebView.CoreWebView2.IsDocumentPlayingAudioChanged");
}
private void CoreWebView2OnIsMutedChanged(object? sender, object e)
{
Debug.Print($"WebView.CoreWebView2.IsMutedChanged:");
}
private async void CoreWebView2OnNavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.NavigationCompleted: {nameof(e.NavigationId)} = {e.NavigationId}, {nameof(e.IsSuccess)} = {e.IsSuccess}");
}
private void CoreWebView2OnNavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.NavigationStarting: {nameof(e.NavigationId)} = {e.NavigationId}, {e.Uri}");
}
private void CoreWebView2OnNewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.NewWindowRequested: {nameof(e.Name)} = {e.Name}, {nameof(e.Uri)} = {e.Uri}");
}
private void CoreWebView2OnPermissionRequested(object? sender, CoreWebView2PermissionRequestedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.PermissionRequested: {nameof(e.PermissionKind)} = {e.PermissionKind}, {nameof(e.State)} = {e.State}, {nameof(e.Uri)} = {e.Uri}");
}
private void CoreWebView2OnProcessFailed(object? sender, CoreWebView2ProcessFailedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.ProcessFailed: {nameof(e.ExitCode)} = {e.ExitCode}, {nameof(e.Reason)} = {e.Reason}, {nameof(e.ProcessDescription)} = {e.ProcessDescription}");
}
private void CoreWebView2OnScriptDialogOpening(object? sender, CoreWebView2ScriptDialogOpeningEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.ScriptDialogOpening: {nameof(e.Kind)} = {e.Kind}, {nameof(e.ResultText)} = {e.ResultText}, {nameof(e.DefaultText)} = {e.DefaultText}, {nameof(e.Message)} = {e.Message}, {nameof(e.Uri)} = {e.Uri}");
}
private void CoreWebView2OnServerCertificateErrorDetected(object? sender, CoreWebView2ServerCertificateErrorDetectedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.ServerCertificateErrorDetected: {nameof(e.Action)} = {e.Action}, {nameof(e.ErrorStatus)} = {e.ErrorStatus}, {nameof(e.ServerCertificate.DisplayName)} = {e.ServerCertificate.DisplayName}, {nameof(e.RequestUri)} = {e.RequestUri}");
}
private void CoreWebView2OnSourceChanged(object? sender, CoreWebView2SourceChangedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.SourceChanged: {nameof(e.IsNewDocument)} = {e.IsNewDocument}");
}
private void CoreWebView2OnStatusBarTextChanged(object? sender, object e)
{
Debug.Print($"WebView.CoreWebView2.StatusBarTextChanged: {(sender as CoreWebView2)?.StatusBarText}");
}
private void CoreWebView2OnWebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.WebMessageReceived: {nameof(e.Source)} = {e.Source}, {nameof(e.WebMessageAsJson)} = {e.WebMessageAsJson}");
}
private void CoreWebView2OnWebResourceRequested(object? sender, CoreWebView2WebResourceRequestedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.WebResourceRequested: {nameof(e.ResourceContext)} = {e.ResourceContext}, {nameof(e.Response.StatusCode)} = {e.Response.StatusCode}, {nameof(e.Request.Uri)} = {e.Request.Uri}");
}
private async void CoreWebView2OnWebResourceResponseReceived(object? sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
Debug.Print($"WebView.CoreWebView2.WebResourceResponseReceived: {nameof(e.Response.StatusCode)} = {e.Response.StatusCode}, {nameof(e.Request.Uri)} = {e.Request.Uri}");
var requestContent = "";
var responstContent = "";
Exception? requestContentException = null;
Exception? responseContentException = null;
try
{
if (e.Request.Content is not null)
{
using var req = new StreamReader(e.Request.Content);
requestContent = await req.ReadToEndAsync();
}
}
catch (Exception exception)
{
requestContentException = exception;
}
try
{
await using var stream = await e.Response.GetContentAsync();
if (stream is not null)
{
using var reader = new StreamReader(stream);
responstContent = await reader.ReadToEndAsync();
}
}
catch (Exception exception)
{
responseContentException = exception;
}
// Debugger.Break();
}
private void CoreWebView2OnWindowCloseRequested(object? sender, object e)
{
Debug.Print($"WebView.CoreWebView2.WindowCloseRequested:");
}
}
関連記事
新着記事