Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround needed for WebView in UNO framework for WebAssembly

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).

like image 372
Syntaxrabbit Avatar asked Apr 16 '19 08:04

Syntaxrabbit


2 Answers

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.

like image 90
Carl de Billy Avatar answered Nov 19 '22 09:11

Carl de Billy


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}" />
like image 44
Syntaxrabbit Avatar answered Nov 19 '22 09:11

Syntaxrabbit