diff --git a/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.19041/PublicAPI.Unshipped.txt b/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.19041/PublicAPI.Unshipped.txt
index f3b5d5ef555b..348e8a524d70 100644
--- a/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.19041/PublicAPI.Unshipped.txt
+++ b/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.19041/PublicAPI.Unshipped.txt
@@ -33,8 +33,6 @@ Microsoft.AspNetCore.Components.WebView.Maui.RootComponent.Selector.set -> void
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.JSComponents.get -> Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore!
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.RootComponentsCollection(Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents) -> void
-Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager
-Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.WinUIWebViewManager(Microsoft.UI.Xaml.Controls.WebView2! webview, System.IServiceProvider! services, Microsoft.AspNetCore.Components.Dispatcher! dispatcher, Microsoft.Extensions.FileProviders.IFileProvider! fileProvider, Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents, string! hostPageRelativePath, string! contentRootDir, Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! webViewHandler) -> void
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.Url.get -> System.Uri!
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.UrlLoadingStrategy.get -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
@@ -43,12 +41,9 @@ Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.CancelLoad = 2 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenExternally = 0 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenInWebView = 1 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
-Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager
Microsoft.Extensions.DependencyInjection.BlazorWebViewServiceCollectionExtensions
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.CreatePlatformView() -> Microsoft.UI.Xaml.Controls.WebView2!
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.DisconnectHandler(Microsoft.UI.Xaml.Controls.WebView2! platformView) -> void
-override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.HandleWebResourceRequest(Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs! eventArgs) -> System.Threading.Tasks.Task!
-override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.QueueBlazorStart() -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapHostPage(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapNotifyUrlLoading(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapRootComponents(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
diff --git a/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.20348/PublicAPI.Unshipped.txt b/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.20348/PublicAPI.Unshipped.txt
index f3b5d5ef555b..348e8a524d70 100644
--- a/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.20348/PublicAPI.Unshipped.txt
+++ b/src/BlazorWebView/src/Maui/PublicAPI/net6.0-windows10.0.20348/PublicAPI.Unshipped.txt
@@ -33,8 +33,6 @@ Microsoft.AspNetCore.Components.WebView.Maui.RootComponent.Selector.set -> void
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.JSComponents.get -> Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore!
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.RootComponentsCollection(Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents) -> void
-Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager
-Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.WinUIWebViewManager(Microsoft.UI.Xaml.Controls.WebView2! webview, System.IServiceProvider! services, Microsoft.AspNetCore.Components.Dispatcher! dispatcher, Microsoft.Extensions.FileProviders.IFileProvider! fileProvider, Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents, string! hostPageRelativePath, string! contentRootDir, Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! webViewHandler) -> void
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.Url.get -> System.Uri!
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.UrlLoadingStrategy.get -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
@@ -43,12 +41,9 @@ Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.CancelLoad = 2 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenExternally = 0 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenInWebView = 1 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
-Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager
Microsoft.Extensions.DependencyInjection.BlazorWebViewServiceCollectionExtensions
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.CreatePlatformView() -> Microsoft.UI.Xaml.Controls.WebView2!
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.DisconnectHandler(Microsoft.UI.Xaml.Controls.WebView2! platformView) -> void
-override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.HandleWebResourceRequest(Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs! eventArgs) -> System.Threading.Tasks.Task!
-override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.QueueBlazorStart() -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapHostPage(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapNotifyUrlLoading(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapRootComponents(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
diff --git a/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs b/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs
index 385214502e7d..9154c07ec663 100644
--- a/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs
+++ b/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs
@@ -7,7 +7,6 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Web.WebView2.Core;
using Windows.ApplicationModel;
-using Windows.Storage;
using Windows.Storage.Streams;
using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2;
@@ -17,7 +16,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
/// An implementation of that uses the Edge WebView2 browser control
/// to render web content in WinUI applications.
///
- public class WinUIWebViewManager : WebView2WebViewManager
+ internal class WinUIWebViewManager : WebView2WebViewManager
{
private readonly WebView2Control _webview;
private readonly string _hostPageRelativePath;
@@ -36,7 +35,6 @@ static WinUIWebViewManager()
}
}
-#pragma warning disable RS0022
///
/// Initializes a new instance of
///
@@ -63,7 +61,6 @@ public WinUIWebViewManager(
_hostPageRelativePath = hostPageRelativePath;
_contentRootDir = contentRootDir;
}
-#pragma warning restore RS0022
///
protected override async Task HandleWebResourceRequest(CoreWebView2WebResourceRequestedEventArgs eventArgs)
diff --git a/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs b/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs
index 353404b1e4fa..26fd2bde968e 100644
--- a/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs
+++ b/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs
@@ -25,9 +25,9 @@ public static class BlazorWebViewServiceCollectionExtensions
/// The .
/// The .
#if WEBVIEW2_WINFORMS
- public static IServiceCollection AddWindowsFormsBlazorWebView(this IServiceCollection services)
+ public static IWindowsFormsBlazorWebViewBuilder AddWindowsFormsBlazorWebView(this IServiceCollection services)
#elif WEBVIEW2_WPF
- public static IServiceCollection AddWpfBlazorWebView(this IServiceCollection services)
+ public static IWpfBlazorWebViewBuilder AddWpfBlazorWebView(this IServiceCollection services)
#elif WEBVIEW2_MAUI
public static IServiceCollection AddMauiBlazorWebView(this IServiceCollection services)
#else
@@ -39,12 +39,14 @@ public static IServiceCollection AddMauiBlazorWebView(this IServiceCollection se
#if WEBVIEW2_MAUI
services.TryAddSingleton();
services.ConfigureMauiHandlers(static handlers => handlers.AddHandler());
+ return services;
#elif WEBVIEW2_WINFORMS
services.TryAddSingleton();
+ return new WindowsFormsBlazorWebViewBuilder(services);
#elif WEBVIEW2_WPF
services.TryAddSingleton();
+ return new WpfBlazorWebViewBuilder(services);
#endif
- return services;
}
///
diff --git a/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs b/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs
index bc025c65c44b..1cba9ae3c634 100644
--- a/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs
+++ b/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs
@@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Components.WebView.WebView2
/// An implementation of that uses the Edge WebView2 browser control
/// to render web content.
///
- public class WebView2WebViewManager : WebViewManager
+ internal class WebView2WebViewManager : WebViewManager
{
// Using an IP address means that WebView2 doesn't wait for any DNS resolution,
// making it substantially faster. Note that this isn't real HTTP traffic, since
@@ -65,7 +65,7 @@ public class WebView2WebViewManager : WebViewManager
private readonly Task _webviewReadyTask;
#if WEBVIEW2_WINFORMS || WEBVIEW2_WPF
- private protected CoreWebView2Environment _coreWebView2Environment;
+ private protected CoreWebView2Environment? _coreWebView2Environment;
private readonly Action _urlLoading;
private readonly Action _blazorWebViewInitializing;
private readonly Action _blazorWebViewInitialized;
@@ -273,7 +273,7 @@ protected virtual Task HandleWebResourceRequest(CoreWebView2WebResourceRequested
{
var headerString = GetHeaderString(headers);
- eventArgs.Response = _coreWebView2Environment.CreateWebResourceResponse(content, statusCode, statusMessage, headerString);
+ eventArgs.Response = _coreWebView2Environment!.CreateWebResourceResponse(content, statusCode, statusMessage, headerString);
}
#elif WEBVIEW2_MAUI
// No-op here because all the work is done in the derived WinUIWebViewManager
@@ -288,7 +288,7 @@ protected virtual void QueueBlazorStart()
{
}
- private void CoreWebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs args)
+ private void CoreWebView2_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs args)
{
if (Uri.TryCreate(args.Uri, UriKind.RelativeOrAbsolute, out var uri))
{
@@ -309,7 +309,7 @@ private void CoreWebView2_NavigationStarting(object sender, CoreWebView2Navigati
}
}
- private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs args)
+ private void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs args)
{
// Intercept _blank target tags to always open in device browser.
// The ExternalLinkCallback is not invoked.
@@ -349,7 +349,7 @@ private void ApplyDefaultWebViewSettings(BlazorWebViewDeveloperTools devTools)
}
#if WEBVIEW2_WINFORMS || WEBVIEW2_WPF
- private static string GetWebView2UserDataFolder()
+ private static string? GetWebView2UserDataFolder()
{
if (Assembly.GetEntryAssembly() is { } mainAssembly)
{
diff --git a/src/BlazorWebView/src/WindowsForms/BlazorWebView.cs b/src/BlazorWebView/src/WindowsForms/BlazorWebView.cs
index 3828d82ebec9..213b562d6768 100644
--- a/src/BlazorWebView/src/WindowsForms/BlazorWebView.cs
+++ b/src/BlazorWebView/src/WindowsForms/BlazorWebView.cs
@@ -4,6 +4,7 @@
using System;
using System.Collections.Specialized;
using System.ComponentModel;
+using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -21,9 +22,9 @@ namespace Microsoft.AspNetCore.Components.WebView.WindowsForms
public class BlazorWebView : ContainerControl
{
private readonly WebView2Control _webview;
- private WebView2WebViewManager _webviewManager;
- private string _hostPage;
- private IServiceProvider _services;
+ private WebView2WebViewManager? _webviewManager;
+ private string? _hostPage;
+ private IServiceProvider? _services;
///
/// Creates a new instance of .
@@ -52,12 +53,6 @@ public BlazorWebView()
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public WebView2Control WebView => _webview;
- ///
- /// Returns the current used by this control. This property is null
- /// until after the XYZ event is raised.
- ///
- public WebView2WebViewManager WebViewManager => _webviewManager;
-
private WindowsFormsDispatcher ComponentsDispatcher { get; }
///
@@ -74,7 +69,7 @@ protected override void OnCreateControl()
///
[Category("Behavior")]
[Description(@"Path to the host page within the application's static files. Example: wwwroot\index.html.")]
- public string HostPage
+ public string? HostPage
{
get => _hostPage;
set
@@ -102,9 +97,10 @@ public string HostPage
///
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+ [DisallowNull]
public IServiceProvider Services
{
- get => _services;
+ get => _services!;
set
{
_services = value;
@@ -118,21 +114,21 @@ public IServiceProvider Services
///
[Category("Action")]
[Description("Allows customizing how links are opened. By default, opens internal links in the webview and external links in an external app.")]
- public EventHandler UrlLoading;
+ public EventHandler? UrlLoading;
///
/// Allows customizing the web view before it is created.
///
[Category("Action")]
[Description("Allows customizing the web view before it is created.")]
- public EventHandler BlazorWebViewInitializing;
+ public EventHandler? BlazorWebViewInitializing;
///
/// Allows customizing the web view after it is created.
///
[Category("Action")]
[Description("Allows customizing the web view after it is created.")]
- public EventHandler BlazorWebViewInitialized;
+ public EventHandler? BlazorWebViewInitialized;
private void OnHostPagePropertyChanged() => StartWebViewCoreIfPossible();
@@ -165,14 +161,14 @@ private void StartWebViewCoreIfPossible()
var entryAssemblyLocation = Assembly.GetEntryAssembly()?.Location;
if (!string.IsNullOrEmpty(entryAssemblyLocation))
{
- appRootDir = Path.GetDirectoryName(entryAssemblyLocation);
+ appRootDir = Path.GetDirectoryName(entryAssemblyLocation)!;
}
else
{
appRootDir = Environment.CurrentDirectory;
}
- var hostPageFullPath = Path.GetFullPath(Path.Combine(appRootDir, HostPage));
- var contentRootDirFullPath = Path.GetDirectoryName(hostPageFullPath);
+ var hostPageFullPath = Path.GetFullPath(Path.Combine(appRootDir, HostPage!)); // HostPage is nonnull because RequiredStartupPropertiesSet is checked above
+ var contentRootDirFullPath = Path.GetDirectoryName(hostPageFullPath)!;
var hostPageRelativePath = Path.GetRelativePath(contentRootDirFullPath, hostPageFullPath);
var fileProvider = CreateFileProvider(contentRootDirFullPath);
@@ -196,7 +192,7 @@ private void StartWebViewCoreIfPossible()
_webviewManager.Navigate("/");
}
- private void HandleRootComponentsCollectionChanged(object sender, NotifyCollectionChangedEventArgs eventArgs)
+ private void HandleRootComponentsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs eventArgs)
{
// If we haven't initialized yet, this is a no-op
if (_webviewManager != null)
@@ -204,8 +200,8 @@ private void HandleRootComponentsCollectionChanged(object sender, NotifyCollecti
// Dispatch because this is going to be async, and we want to catch any errors
_ = ComponentsDispatcher.InvokeAsync(async () =>
{
- var newItems = eventArgs.NewItems.Cast();
- var oldItems = eventArgs.OldItems.Cast();
+ var newItems = (eventArgs.NewItems ?? Array.Empty