Skip to content

Commit 02146ed

Browse files
committed
Add HtmlSourceProperty & Remove VerifyInitializedGuard
1 parent 90fcc0c commit 02146ed

File tree

3 files changed

+61
-31
lines changed

3 files changed

+61
-31
lines changed

src/Avalonia.WebView2/Avalonia.WebView2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
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.
1111
</Description>
1212
<PackageTags>Web WebView Native native package Edge avalonia avaloniaui dotnet framework core Webview2</PackageTags>
13-
<Version>1.0.1264.42-preview.220712.8</Version>
13+
<Version>1.0.1264.42-preview.220712.9</Version>
1414
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
1515
</PropertyGroup>
1616

src/Avalonia.WebView2/WebView2.AvaloniaProperty.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ partial class WebView2
1717
/// </summary>
1818
public static readonly DirectProperty<WebView2, Uri?> SourceProperty = AvaloniaProperty.RegisterDirect<WebView2, Uri?>(nameof(Source), x => x._source, (x, y) => x.Source = y);
1919

20+
public static readonly DirectProperty<WebView2, string?> HtmlSourceProperty = AvaloniaProperty.RegisterDirect<WebView2, string?>(nameof(HtmlSource), x => x._htmlSource, (x, y) => x.HtmlSource = y);
21+
2022
/// <summary>
2123
/// The <see cref="AvaloniaProperty" /> which backs the <see cref="CanGoBack" /> property.
2224
/// </summary>

src/Avalonia.WebView2/WebView2.cs

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ protected override void OnInitialized()
149149
static readonly Color _defaultBackgroundColorDefaultValue = Color.White;
150150
Color _defaultBackgroundColor;
151151
Uri? _source;
152+
string? _htmlSource;
152153
bool _browserCrashed;
153154
readonly ImplicitInitGate _implicitInitGate = new();
154155

@@ -158,8 +159,7 @@ protected virtual void Dispose(bool disposing)
158159
{
159160
if (disposing)
160161
{
161-
if (IsInitialized)
162-
UnsubscribeHandlersAndCloseController();
162+
UnsubscribeHandlersAndCloseController();
163163
}
164164

165165
disposedValue = true;
@@ -174,7 +174,6 @@ public void Dispose()
174174

175175
void UnsubscribeHandlersAndCloseController(bool browserCrashed = false)
176176
{
177-
IsInitialized = false;
178177
_browserCrashed = browserCrashed;
179178
if (!_browserCrashed)
180179
{
@@ -397,14 +396,17 @@ async Task InitCoreWebView2Async(CoreWebView2Environment? environment = null, Co
397396
sender.CoreWebView2.ProcessFailed += new EventHandler<CoreWebView2ProcessFailedEventArgs>(sender.CoreWebView2_ProcessFailed);
398397
if (sender.Focusable)
399398
sender._coreWebView2Controller.MoveFocus(CoreWebView2MoveFocusReason.Programmatic);
400-
int num = sender._source != null ? 1 : 0;
401-
if (sender._source == null)
402-
sender._source = new Uri(sender.CoreWebView2.Source);
403-
sender.IsInitialized = true;
399+
404400
sender.CoreWebView2InitializationCompleted?.Invoke(sender, new CoreWebView2InitializationCompletedEventArgs());
405-
if (num == 0)
406-
return;
407-
sender.CoreWebView2.Navigate(sender._source.AbsoluteUri);
401+
402+
if (sender._source != null)
403+
{
404+
sender.CoreWebView2.Navigate(sender._source.AbsoluteUri);
405+
}
406+
else if (sender._htmlSource != null)
407+
{
408+
sender.CoreWebView2.NavigateToString(sender._htmlSource);
409+
}
408410
}
409411
catch (Exception ex)
410412
{
@@ -478,7 +480,7 @@ protected virtual void IsVisibleChanged(EventArgs e)
478480
protected override void OnGotFocus(GotFocusEventArgs e)
479481
{
480482
base.OnGotFocus(e);
481-
if (IsInitialized)
483+
if (_coreWebView2Controller != null)
482484
{
483485
if (!_browserCrashed)
484486
{
@@ -500,11 +502,6 @@ protected override void OnGotFocus(GotFocusEventArgs e)
500502
#endif
501503
}
502504

503-
/// <summary>
504-
/// True if initialization finished successfully and the control is not disposed yet.
505-
/// </summary>
506-
protected new bool IsInitialized { get; set; }
507-
508505
#if !DISABLE_WEBVIEW2_CORE
509506
/// <summary>
510507
/// The underlying CoreWebView2. Use this property to perform more operations on the WebView2 content than is exposed
@@ -608,6 +605,7 @@ public Uri? Source
608605
}
609606
if (_source == null || _source.AbsoluteUri != value.AbsoluteUri)
610607
{
608+
_htmlSource = null;
611609
SetAndRaise(SourceProperty, ref _source, value);
612610
#if !DISABLE_WEBVIEW2_CORE
613611
if (CoreWebView2 != null) CoreWebView2.Navigate(value.AbsoluteUri);
@@ -620,6 +618,37 @@ public Uri? Source
620618
}
621619
}
622620

621+
[Browsable(true)]
622+
public string? HtmlSource
623+
{
624+
get => _htmlSource;
625+
set
626+
{
627+
if (value == null)
628+
{
629+
if (_htmlSource == null)
630+
{
631+
return;
632+
}
633+
throw new NotImplementedException("The HtmlSource property cannot be set to null.");
634+
}
635+
else
636+
{
637+
if (_htmlSource == null || _htmlSource != value)
638+
{
639+
_source = null;
640+
SetAndRaise(HtmlSourceProperty, ref _htmlSource, value);
641+
#if !DISABLE_WEBVIEW2_CORE
642+
if (CoreWebView2 != null) CoreWebView2.NavigateToString(value);
643+
#endif
644+
}
645+
#if !DISABLE_WEBVIEW2_CORE
646+
_implicitInitGate.RunWhenOpen(() => EnsureCoreWebView2Async());
647+
#endif
648+
}
649+
}
650+
}
651+
623652
/// <summary>
624653
/// Returns true if the webview can navigate to a next page in the
625654
/// navigation history via the <see cref="M:Avalonia.Controls.WebView2.GoForward" /> method.
@@ -689,13 +718,14 @@ public Color DefaultBackgroundColor
689718
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.ExecuteScriptAsync(System.String)" />
690719
public async Task<string> ExecuteScriptAsync(string script)
691720
{
692-
VerifyInitializedGuard();
693721
VerifyBrowserNotCrashedGuard();
694722
#if !DISABLE_WEBVIEW2_CORE
695-
return await CoreWebView2!.ExecuteScriptAsync(script);
696-
#else
697-
return await Task.FromResult(string.Empty);
723+
if (CoreWebView2 != null)
724+
{
725+
return await CoreWebView2.ExecuteScriptAsync(script);
726+
}
698727
#endif
728+
return await Task.FromResult(string.Empty);
699729
}
700730

701731
/// <summary>
@@ -707,10 +737,12 @@ public async Task<string> ExecuteScriptAsync(string script)
707737
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.Reload" />
708738
public void Reload()
709739
{
710-
VerifyInitializedGuard();
711740
VerifyBrowserNotCrashedGuard();
712741
#if !DISABLE_WEBVIEW2_CORE
713-
CoreWebView2!.Reload();
742+
if (CoreWebView2 != null)
743+
{
744+
CoreWebView2!.Reload();
745+
}
714746
#endif
715747
}
716748

@@ -750,10 +782,12 @@ public void GoBack()
750782
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.NavigateToString(System.String)" />
751783
public void NavigateToString(string htmlContent)
752784
{
753-
VerifyInitializedGuard();
754785
VerifyBrowserNotCrashedGuard();
755786
#if !DISABLE_WEBVIEW2_CORE
756-
CoreWebView2!.NavigateToString(htmlContent);
787+
if (CoreWebView2 != null)
788+
{
789+
CoreWebView2.NavigateToString(htmlContent);
790+
}
757791
#endif
758792
}
759793

@@ -770,12 +804,6 @@ public void Stop()
770804
{ }
771805
#endif
772806

773-
void VerifyInitializedGuard()
774-
{
775-
if (!IsInitialized)
776-
throw new InvalidOperationException("The instance of CoreWebView2 is uninitialized and unable to complete this operation. See EnsureCoreWebView2Async.");
777-
}
778-
779807
#if !DISABLE_WEBVIEW2_CORE
780808
void VerifyNotClosedGuard()
781809
{

0 commit comments

Comments
 (0)