设置 Windows Phone 8.1 WebView 的基础 URL 并使用 NavigateToString()

Set base URL of Windows Phone 8.1 WebView and use NavigateToString()

在我的应用程序中,我想加载一个 html,其中包含一个带有远程 url 的 iframe。这个 html 总是一样的,除了 iframe 的 src。我所做的是将 html 保存在字符串 中,然后填写 iframe url,然后调用 NavigateToString(html )。我从 REST API 获得 iframe url。我还得到一个 baseUrl。如果我 将这两个 urls 合并成一个绝对 url 并将其设置为 iframe 的 src, 页面将不会加载 由于安全原因(这是一个支付页面)。但是如果我设置它没有baseUrl,当然,webview不知道baseUrl是什么,页面不会加载.

在 Android 中,可以简单地调用 webView.loadDataWithBaseURL。 Windows Phone 8.1 中有这样的东西吗?任何解决方法?

(问题变得更加复杂,因为我还必须将 Cookies 设置到 webview。)

我想出了一个有效的解决方案,实际上它不像我最初想象的那么难用。

我跳过了 NavigateToString。通过使用 NavigateWithHttpRequestMessage,我可以将请求发送到 URL,例如 http://mybackend.com. I can set cookies as well (more information on this: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/a451d411-9312-4d40-80ee-112e166144ab/how-to-send-auth-cookie-in-webview-request?forum=winappswithcsharp)。到目前为止我有:

  1. 基础URL集

  2. cookie 设置。

不过,我还没有设置本地 html 字符串。为此,在 WebView 的 NavigationCompleted 事件中,我 运行 一个像这样的 JS 脚本:

await wvSecurePay.InvokeScriptAsync("eval", new string[] { "document.getElementsByTagName('html')[0].innerHTML = '" + htmlToLoad + "';"})

这会将当前 html 内容替换为我要加载的内容。因此,我的 html 在具有我需要的域的页面上包含带有相对 url 的 iframe。