diff --git a/WpfExtended.Blazor/Exceptions/CoreWebView2Exception.cs b/WpfExtended.Blazor/Exceptions/CoreWebView2Exception.cs new file mode 100644 index 0000000..fdd079d --- /dev/null +++ b/WpfExtended.Blazor/Exceptions/CoreWebView2Exception.cs @@ -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)); +} diff --git a/WpfExtended.Blazor/Launch/BlazorHostWindow.xaml.cs b/WpfExtended.Blazor/Launch/BlazorHostWindow.xaml.cs index 676d77a..f1c7c68 100644 --- a/WpfExtended.Blazor/Launch/BlazorHostWindow.xaml.cs +++ b/WpfExtended.Blazor/Launch/BlazorHostWindow.xaml.cs @@ -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? BlazorWebViewInitialized; + + public event EventHandler? BlazorWebViewInitializing; + + public event EventHandler? CoreWebView2InitializationCompleted; + + public event EventHandler? 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); + } } diff --git a/WpfExtended.Blazor/Launch/BlazorHybridApplication.cs b/WpfExtended.Blazor/Launch/BlazorHybridApplication.cs index e41d9f1..bd40680 100644 --- a/WpfExtended.Blazor/Launch/BlazorHybridApplication.cs +++ b/WpfExtended.Blazor/Launch/BlazorHybridApplication.cs @@ -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 : ExtendedApplication(); + 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; + } + } } diff --git a/WpfExtended.Blazor/WpfExtended.Blazor.csproj b/WpfExtended.Blazor/WpfExtended.Blazor.csproj index 5f7d1b6..48abd85 100644 --- a/WpfExtended.Blazor/WpfExtended.Blazor.csproj +++ b/WpfExtended.Blazor/WpfExtended.Blazor.csproj @@ -4,7 +4,8 @@ net9.0-windows true true - 0.8.0 + 0.8.1 + enable latest Extension library for Blazor Hybrid running on Windows Presentation Platform. diff --git a/WpfExtended/WpfExtended.csproj b/WpfExtended/WpfExtended.csproj index f98b530..5ead65b 100644 --- a/WpfExtended/WpfExtended.csproj +++ b/WpfExtended/WpfExtended.csproj @@ -4,7 +4,7 @@ net9.0-windows true true - 0.8.0 + 0.8.1 latest Extension library for Windows Presentation Platform.