问题
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 -->
<h1>It works!</h1>
</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