我有一个MAUI WebView,我使用它作为容器来显示html格式的内容,该内容来自API并包含编码的html内容
在我的 .xaml 文件中:
<WebView HeightRequest="300">
<WebView.Source>
<HtmlWebViewSource Html="{Binding MyHtml}" />
</WebView.Source>
</WebView>
在我的 ViewModel .cs 文件中:
[ObservableProperty]
private string myHtml;
...
MyHtml= APIModel.HtmlString.ToString(); // this contains the following string: "<strong><span style="font-size: 20px;">M</span></strong><span style="color: rgb(247, 150, 70);">e</span>mo<span style="font-size: 24px; font-family: Verdana;"><em>6</em></span>"
它应该以适当的格式显示 html 内容,但我不确定为什么它以解码格式显示,如下图所示:
提前感谢您的帮助!
根据我的测试,MyHtml 对象中的绑定内容在其编码的 html 格式中保持正确,但由于某种原因,在绑定时显示以编码格式显示
如果你想在代码后面设置Html字符串,你可以做一些改变。
注:
C# 中的<
应该等同于 C# 中的 <
C# 中的 >
应该等同于 C# 中的 >
C# 中的 "
应该等同于 C# 中的 \"
所以当你在代码后面定义Html字符串时,你应该更改为相应的格式。你的 C# 中的 Html 应该是这样的:
MyHtml = "<strong><span style=\"font-size: 20px;\">M</span></strong><span style=\"color: rgb(247, 150, 70);\">e</span>mo<span style=\"font-size: 24px; font-family: Verdana;\"><em>6</em></span>";
您可以使用Replace方法来转换:
NewHtml = MyHtml.Replace("<","<").Replace(">",">").Replace(""",""");
更多信息,您可以参考显示本地HTML