Expose CoreWebView2 initialization logic

This commit is contained in:
2025-08-03 11:12:36 +02:00
parent 103ee09b0e
commit ca9e682d04
5 changed files with 104 additions and 3 deletions
@@ -0,0 +1,9 @@
using Microsoft.Web.WebView2.Core;
using System;
namespace WpfExtended.Blazor.Exceptions;
public sealed class CoreWebView2Exception(CoreWebView2ProcessFailedEventArgs args, string message, Exception? innerException = default) : Exception(message, innerException)
{
public CoreWebView2ProcessFailedEventArgs Args { get; } = args ?? throw new ArgumentNullException(nameof(args));
}
@@ -1,4 +1,7 @@
using System;
using Microsoft.AspNetCore.Components.WebView;
using Microsoft.AspNetCore.Components.WebView.Wpf;
using Microsoft.Web.WebView2.Core;
using System;
using System.Windows;
namespace WpfExtended.Blazor.Launch;
@@ -21,6 +24,14 @@ public partial class BlazorHostWindow : Window
public static readonly DependencyProperty AppTypeProperty = DependencyProperty.Register(
nameof(AppType), typeof(Type), typeof(BlazorHostWindow));
public event EventHandler<BlazorWebViewInitializedEventArgs>? BlazorWebViewInitialized;
public event EventHandler<BlazorWebViewInitializingEventArgs>? BlazorWebViewInitializing;
public event EventHandler<CoreWebView2InitializationCompletedEventArgs>? CoreWebView2InitializationCompleted;
public event EventHandler<CoreWebView2>? CoreWebView2Initialized;
public IServiceProvider Services
{
get => (IServiceProvider)this.GetValue(ServicesProperty);
@@ -63,6 +74,8 @@ public partial class BlazorHostWindow : Window
this.ShowTitleBar = blazorLaunchProperties.ShowTitleBar;
this.HostPage = blazorLaunchProperties.HostPage;
this.AppType = blazorLaunchProperties.AppType;
this.BlazorWebView.BlazorWebViewInitialized += this.BlazorWebView_BlazorWebViewInitialized;
this.BlazorWebView.BlazorWebViewInitializing += this.BlazorWebView_BlazorWebViewInitializing;
}
protected override void OnStateChanged(EventArgs e)
@@ -79,4 +92,27 @@ public partial class BlazorHostWindow : Window
: new Thickness(cornerRadius)
: new Thickness(0, this.ShowTitleBar ? SystemParameters.CaptionHeight : 0, 0, 0);
}
private void BlazorWebView_BlazorWebViewInitialized(object? sender, BlazorWebViewInitializedEventArgs args)
{
this.BlazorWebView.WebView.CoreWebView2InitializationCompleted += this.CoreWebView2_InitializationCompleted;
this.BlazorWebViewInitialized?.Invoke(this, args);
}
private void BlazorWebView_BlazorWebViewInitializing(object? sender, BlazorWebViewInitializingEventArgs args)
{
this.BlazorWebViewInitializing?.Invoke(this, args);
}
private void CoreWebView2_InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
{
this.CoreWebView2InitializationCompleted?.Invoke(this, e);
if (!e.IsSuccess)
{
return;
}
this.CoreWebView2Initialized?.Invoke(this, this.BlazorWebView.WebView.CoreWebView2);
}
}
@@ -1,6 +1,11 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebView;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Web.WebView2.Core;
using System;
using System.Threading.Tasks;
using System.Windows.Extensions;
using WpfExtended.Blazor.Exceptions;
namespace WpfExtended.Blazor.Launch;
@@ -20,4 +25,54 @@ public abstract class BlazorHybridApplication<TApp> : ExtendedApplication<Blazor
services.AddBlazorWebViewDeveloperTools();
}
}
protected override ValueTask ApplicationStarting()
{
var host = this.ServiceProvider.GetRequiredService<BlazorHostWindow>();
host.BlazorWebViewInitializing += (_, e) => this.Host_BlazorWebViewInitializing(e);
host.BlazorWebViewInitialized += (_, e) => this.Host_BlazorWebViewInitialized(e);
host.CoreWebView2InitializationCompleted += (_, e) => this.Host_CoreWebView2InitializationCompleted(e);
host.CoreWebView2Initialized += (_, e) => this.Host_CoreWebView2Initialized(e);
return ValueTask.CompletedTask;
}
protected virtual void Host_CoreWebView2Initialized(CoreWebView2 e)
{
e.ProcessFailed += (_, ex) => this.CoreWebView2_ProcessFailed(ex);
}
protected virtual void Host_CoreWebView2InitializationCompleted(CoreWebView2InitializationCompletedEventArgs e)
{
if (e.InitializationException is not null)
{
this.HandleException(e.InitializationException);
return;
}
if (e.IsSuccess is false)
{
this.HandleException(new InvalidOperationException("CoreWebView2 initialization failed."));
return;
}
}
protected virtual void Host_BlazorWebViewInitialized(BlazorWebViewInitializedEventArgs e)
{
}
protected virtual void Host_BlazorWebViewInitializing(BlazorWebViewInitializingEventArgs e)
{
}
private void CoreWebView2_ProcessFailed(CoreWebView2ProcessFailedEventArgs e)
{
var exception = new CoreWebView2Exception(e, "Encountered exception in CoreWebView2");
if (!this.HandleException(exception))
{
throw exception;
}
}
}
+2 -1
View File
@@ -4,7 +4,8 @@
<TargetFrameworks>net9.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Description>
Extension library for Blazor Hybrid running on Windows Presentation Platform.
+1 -1
View File
@@ -4,7 +4,7 @@
<TargetFrameworks>net9.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<LangVersion>latest</LangVersion>
<Description>Extension library for Windows Presentation Platform.