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

Workaround needed for WebView in UNO framework for WebAssembly

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

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

一个全功能的 webview 不是那么容易完成的:它与浏览器中的 xss 保护有关。

另一个原因仅仅是功能优先级。 Wasm 仍然是 nventive(Uno Platform 背后的 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;
      }
    }
  }

和 XAML:

  <ctl:WasmHtmlContentControl>
    <!-- xml encoded html -->
    &lt;h1&gt;It works!&lt;/h1&gt;
  </ctl:WasmHtmlContentControl>

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

您的代码示例几乎可以正常工作。稍作调整,这是一个可行的解决方案:

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;
            }
        }
    }
}

在XAML中:

<WebAssemblyHtmlControl HtmlContent="{Binding HtmlString}" />