问题描述
目前,我正在开发一个 UNO 平台应用程序,它应该在 WebView
中显示动态创建的 HTML 代码.这在 UWP 和 Android 上运行良好,但不适用于编译的 WebAssembly
.我可以在这里使用某种解决方法吗?我想到了一个简单的 IFRAME
,但显然不可能在 XAML 文件中包含 HTML.还是我错了?
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?
更具体地说:WebView的NavigateToString("<html><head></head><body>BLAH!</body><html>")
方法在 UWP 和 Android 中产生了预期的结果(iOS 未测试).
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).
推荐答案
您的代码示例几乎可以工作了.通过小幅调整,这是一个可行的解决方案:
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;
}
}
}
}
在 XAML 中:
<WebAssemblyHtmlControl HtmlContent="{Binding HtmlString}" />
这篇关于WebAssembly 的 UNO 框架中的 WebView 所需的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!