diff --git a/src/Avalonia.WebView2/Avalonia.WebView2.csproj b/src/Avalonia.WebView2/Avalonia.WebView2.csproj
index 14c9ca7..fb6dbd0 100644
--- a/src/Avalonia.WebView2/Avalonia.WebView2.csproj
+++ b/src/Avalonia.WebView2/Avalonia.WebView2.csproj
@@ -10,7 +10,7 @@
This package is necessary for Avalonia applications. To learn more about package versions checkout https://aka.ms/wv2-packageversion. To learn best practices checkout: https://aka.ms/wv2-bestpractices.
Web WebView Native native package Edge avalonia avaloniaui dotnet framework core Webview2
- 1.0.1264.42-preview.220712.8
+ 1.0.1264.42-preview.220712.9
true
diff --git a/src/Avalonia.WebView2/WebView2.AvaloniaProperty.cs b/src/Avalonia.WebView2/WebView2.AvaloniaProperty.cs
index a62ca92..ca1f2d6 100644
--- a/src/Avalonia.WebView2/WebView2.AvaloniaProperty.cs
+++ b/src/Avalonia.WebView2/WebView2.AvaloniaProperty.cs
@@ -17,6 +17,8 @@ partial class WebView2
///
public static readonly DirectProperty SourceProperty = AvaloniaProperty.RegisterDirect(nameof(Source), x => x._source, (x, y) => x.Source = y);
+ public static readonly DirectProperty HtmlSourceProperty = AvaloniaProperty.RegisterDirect(nameof(HtmlSource), x => x._htmlSource, (x, y) => x.HtmlSource = y);
+
///
/// The which backs the property.
///
diff --git a/src/Avalonia.WebView2/WebView2.cs b/src/Avalonia.WebView2/WebView2.cs
index f28eb83..4b6e9b6 100644
--- a/src/Avalonia.WebView2/WebView2.cs
+++ b/src/Avalonia.WebView2/WebView2.cs
@@ -149,6 +149,7 @@ protected override void OnInitialized()
static readonly Color _defaultBackgroundColorDefaultValue = Color.White;
Color _defaultBackgroundColor;
Uri? _source;
+ string? _htmlSource;
bool _browserCrashed;
readonly ImplicitInitGate _implicitInitGate = new();
@@ -158,8 +159,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
- if (IsInitialized)
- UnsubscribeHandlersAndCloseController();
+ UnsubscribeHandlersAndCloseController();
}
disposedValue = true;
@@ -174,7 +174,6 @@ public void Dispose()
void UnsubscribeHandlersAndCloseController(bool browserCrashed = false)
{
- IsInitialized = false;
_browserCrashed = browserCrashed;
if (!_browserCrashed)
{
@@ -397,14 +396,17 @@ async Task InitCoreWebView2Async(CoreWebView2Environment? environment = null, Co
sender.CoreWebView2.ProcessFailed += new EventHandler(sender.CoreWebView2_ProcessFailed);
if (sender.Focusable)
sender._coreWebView2Controller.MoveFocus(CoreWebView2MoveFocusReason.Programmatic);
- int num = sender._source != null ? 1 : 0;
- if (sender._source == null)
- sender._source = new Uri(sender.CoreWebView2.Source);
- sender.IsInitialized = true;
+
sender.CoreWebView2InitializationCompleted?.Invoke(sender, new CoreWebView2InitializationCompletedEventArgs());
- if (num == 0)
- return;
- sender.CoreWebView2.Navigate(sender._source.AbsoluteUri);
+
+ if (sender._source != null)
+ {
+ sender.CoreWebView2.Navigate(sender._source.AbsoluteUri);
+ }
+ else if (sender._htmlSource != null)
+ {
+ sender.CoreWebView2.NavigateToString(sender._htmlSource);
+ }
}
catch (Exception ex)
{
@@ -478,7 +480,7 @@ protected virtual void IsVisibleChanged(EventArgs e)
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
- if (IsInitialized)
+ if (_coreWebView2Controller != null)
{
if (!_browserCrashed)
{
@@ -500,11 +502,6 @@ protected override void OnGotFocus(GotFocusEventArgs e)
#endif
}
- ///
- /// True if initialization finished successfully and the control is not disposed yet.
- ///
- protected new bool IsInitialized { get; set; }
-
#if !DISABLE_WEBVIEW2_CORE
///
/// The underlying CoreWebView2. Use this property to perform more operations on the WebView2 content than is exposed
@@ -608,6 +605,7 @@ public override void EndInit()
}
if (_source == null || _source.AbsoluteUri != value.AbsoluteUri)
{
+ _htmlSource = null;
SetAndRaise(SourceProperty, ref _source, value);
#if !DISABLE_WEBVIEW2_CORE
if (CoreWebView2 != null) CoreWebView2.Navigate(value.AbsoluteUri);
@@ -620,6 +618,37 @@ public override void EndInit()
}
}
+ [Browsable(true)]
+ public string? HtmlSource
+ {
+ get => _htmlSource;
+ set
+ {
+ if (value == null)
+ {
+ if (_htmlSource == null)
+ {
+ return;
+ }
+ throw new NotImplementedException("The HtmlSource property cannot be set to null.");
+ }
+ else
+ {
+ if (_htmlSource == null || _htmlSource != value)
+ {
+ _source = null;
+ SetAndRaise(HtmlSourceProperty, ref _htmlSource, value);
+#if !DISABLE_WEBVIEW2_CORE
+ if (CoreWebView2 != null) CoreWebView2.NavigateToString(value);
+#endif
+ }
+#if !DISABLE_WEBVIEW2_CORE
+ _implicitInitGate.RunWhenOpen(() => EnsureCoreWebView2Async());
+#endif
+ }
+ }
+ }
+
///
/// Returns true if the webview can navigate to a next page in the
/// navigation history via the method.
@@ -689,13 +718,14 @@ public Color DefaultBackgroundColor
///
public async Task ExecuteScriptAsync(string script)
{
- VerifyInitializedGuard();
VerifyBrowserNotCrashedGuard();
#if !DISABLE_WEBVIEW2_CORE
- return await CoreWebView2!.ExecuteScriptAsync(script);
-#else
- return await Task.FromResult(string.Empty);
+ if (CoreWebView2 != null)
+ {
+ return await CoreWebView2.ExecuteScriptAsync(script);
+ }
#endif
+ return await Task.FromResult(string.Empty);
}
///
@@ -707,10 +737,12 @@ public async Task ExecuteScriptAsync(string script)
///
public void Reload()
{
- VerifyInitializedGuard();
VerifyBrowserNotCrashedGuard();
#if !DISABLE_WEBVIEW2_CORE
- CoreWebView2!.Reload();
+ if (CoreWebView2 != null)
+ {
+ CoreWebView2!.Reload();
+ }
#endif
}
@@ -750,10 +782,12 @@ public void GoBack()
///
public void NavigateToString(string htmlContent)
{
- VerifyInitializedGuard();
VerifyBrowserNotCrashedGuard();
#if !DISABLE_WEBVIEW2_CORE
- CoreWebView2!.NavigateToString(htmlContent);
+ if (CoreWebView2 != null)
+ {
+ CoreWebView2.NavigateToString(htmlContent);
+ }
#endif
}
@@ -770,12 +804,6 @@ public void Stop()
{ }
#endif
- void VerifyInitializedGuard()
- {
- if (!IsInitialized)
- throw new InvalidOperationException("The instance of CoreWebView2 is uninitialized and unable to complete this operation. See EnsureCoreWebView2Async.");
- }
-
#if !DISABLE_WEBVIEW2_CORE
void VerifyNotClosedGuard()
{