提交 2b4b646b 编写于 作者: M Martin Zikmund

chore: All WebView runtime tests pass on Android

上级 cb2b321f
......@@ -45,7 +45,10 @@ public partial class CoreWebView2
}
_processedSource = actualUri;
Source = actualUri.ToString();
if (_owner.SwitchSourceBeforeNavigating)
{
Source = actualUri.ToString();
}
UpdateFromInternalSource();
}
......@@ -53,7 +56,10 @@ public partial class CoreWebView2
public void NavigateToString(string htmlContent)
{
_processedSource = htmlContent;
Source = BlankUrl;
if (_owner.SwitchSourceBeforeNavigating)
{
Source = BlankUrl;
}
UpdateFromInternalSource();
}
......@@ -66,7 +72,10 @@ public partial class CoreWebView2
}
_processedSource = requestMessage;
Source = requestMessage.RequestUri.ToString();
if (_owner.SwitchSourceBeforeNavigating)
{
Source = requestMessage.RequestUri.ToString();
}
UpdateFromInternalSource();
}
......
......@@ -2,5 +2,7 @@
internal interface IWebView
{
bool SwitchSourceBeforeNavigating { get; }
bool IsLoaded { get; }
}
......@@ -40,6 +40,8 @@ public partial class WebView : Control, IWebView
bool IWebView.IsLoaded => IsLoaded;
bool IWebView.SwitchSourceBeforeNavigating => true;
protected override void OnApplyTemplate() => CoreWebView2.OnOwnerApplyTemplate();
public void Navigate(global::System.Uri source) => CoreWebView2.Navigate(source.ToString());
......@@ -54,18 +56,29 @@ public partial class WebView : Control, IWebView
public void Stop() => CoreWebView2.Stop();
public IAsyncOperation<string?> InvokeScriptAsync(string scriptName, IEnumerable<string> arguments)
public IAsyncOperation<string?> InvokeScriptAsync(string scriptName, IEnumerable<string> arguments) =>
AsyncOperation.FromTask(ct => InvokeScriptAsync(ct, scriptName, arguments.ToArray()));
public async Task<string?> InvokeScriptAsync(CancellationToken ct, string script, string[] arguments)
{
var argumentString = ConcatenateJavascriptArguments(arguments);
var javaScript = string.Format(CultureInfo.InvariantCulture, "{0}(\"{1}\")", scriptName, argumentString);
return CoreWebView2.ExecuteScriptAsync(javaScript);
var javaScript = string.Format(CultureInfo.InvariantCulture, "{0}(\"{1}\")", script, argumentString);
return AdjustInvokeScriptResult(await CoreWebView2.ExecuteScriptAsync(javaScript));
}
public async Task<string> InvokeScriptAsync(CancellationToken ct, string script, string[] arguments)
private string? AdjustInvokeScriptResult(string result)
{
var argumentString = ConcatenateJavascriptArguments(arguments);
var javaScript = string.Format(CultureInfo.InvariantCulture, "{0}(\"{1}\")", script, argumentString);
return await CoreWebView2.ExecuteScriptAsync(javaScript);
if (result is null)
{
return null;
}
if (result.StartsWith("\"", StringComparison.Ordinal) && result.EndsWith("\"", StringComparison.Ordinal))
{
return result.Substring(1, result.Length - 2);
}
return "";
}
public void NavigateWithHttpRequestMessage(global::System.Net.Http.HttpRequestMessage requestMessage) =>
......
......@@ -42,6 +42,8 @@ public partial class WebView2 : Control, IWebView
bool IWebView.IsLoaded => IsLoaded;
bool IWebView.SwitchSourceBeforeNavigating => false; // WebView2 switches source only when navigation completes.
protected override void OnApplyTemplate() => CoreWebView2.OnOwnerApplyTemplate();
private void WebView2_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) =>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册