Search code examples
c#webviewwebassemblyuno-platform

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


Solution

  • 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}" />