Workaround needed for WebView in UNO framework for WebAssembly

被刻印的时光 ゝ 提交于 2019-12-10 18:23:26

问题


Currently, I am working on a UNO platform application which should display dynamically created HTML code in a WebView. This is working fine on UWP and Android, but not in the compiled WebAssembly. Is there some kind of workaround I could use here? I thought about a simple IFRAME, but obviously there is no possibility to include HTML in the XAML file. Or am I wrong?

To be more specific: The WebView's NavigateToString("<html><head></head><body>BLAH!</body><html>") method leads to the desired results in UWP and Android (iOS not tested).


回答1:


A full featured webview can't be done easily: it's related to xss protections in browsers.

Another reason is simply features priorization. Wasm is still a new target for nventive (the cie behind Uno Platform) and some features still missing to reach parity with iOS and Android. About this, please open an issue on github and explain what is missing for you.

But you can still do something. You can create a custom control in your application like that:

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

And the XAML:

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

Maybe a <![CDATA[ ... ]]> could work... never tried.




回答2:


Your code sample almost worked. With small adjustments, this is a working solution:

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

And in XAML:

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


来源:https://stackoverflow.com/questions/55703910/workaround-needed-for-webview-in-uno-framework-for-webassembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!