WebAssembly 的 UNO 框架中的 WebView 所需的解决方法

Syn*_*bit 5 c# webview webassembly uno-platform

目前,我正在开发一个 UNO 平台应用程序,它应该以 .html 格式显示动态创建的 HTML 代码WebView。这在 UWP 和 Android 上运行良好,但在编译的WebAssembly. 我可以在这里使用某种解决方法吗?我想到了一个简单的IFRAME,但显然不可能在 XAML 文件中包含 HTML。还是我错了?

更具体地说:WebView 的NavigateToString("<html><head></head><body>BLAH!</body><html>")方法会在 UWP 和 Android(iOS 未测试)中产生所需的结果。

Car*_*lly 5

功能齐全的 webview 不容易完成:它与浏览器中的 xss 保护有关。

另一个原因只是功能优先级。Wasm 仍然是 nventive(Uno 平台背后的 cie)的新目标,并且仍然缺少一些功能,无法与 iOS 和 Android 相提并论。关于这个,请在​​github上打开一个问题并解释你缺少什么。

但是你仍然可以做一些事情。您可以在应用程序中创建自定义控件,如下所示:

  [ContentProperty(nameof(HtmlContent))]
  public class WasmHtmlContentControl : Control
  {
    public WasmHtmlContentControl()
     : base(htmlTag: "div") // the root HTML tag of your content
    {
    }

    private string _html;

    public string HtmlContent
    {
      get => _html;
      set
      {
        base.SetHtmlContent(html); // this is a protected method on Wasm target
        _html = value;
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

和 XAML:

  <ctl:WasmHtmlContentControl>
    <!-- xml encoded html -->
    &lt;h1&gt;It works!&lt;/h1&gt;
  </ctl:WasmHtmlContentControl>
Run Code Online (Sandbox Code Playgroud)

也许<![CDATA[......]]>可以工作......从未尝试过。

  • 是的,您可以使用 DependencyProperty 设置内容...这应该很容易做到。`base.SetHtmlContent()` 将替换节点的全部内容。如果您只想更新其中的一部分,则需要注入 javascript。 (2认同)

Syn*_*bit 2

您的代码示例几乎可以工作。经过一些小的调整,这是一个可行的解决方案:

using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace MyProjectNamespace
{
    [ContentProperty(Name = nameof(HtmlContent))]
    public class WebAssemblyHtmlControl : Control
    {
        public WebAssemblyHtmlControl()
         : base(htmlTag: "div") // the root HTML tag of your content
        {
        }

        private string _html;

        public string HtmlContent
        {
            get => _html;
            set
            {
                base.SetHtmlContent(value); // this is a protected method on Wasm target   
                _html = value;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在 XAML 中:

<WebAssemblyHtmlControl HtmlContent="{Binding HtmlString}" />
Run Code Online (Sandbox Code Playgroud)