MAUI WebView 在 ObservableProperty 绑定中显示已解码的 Html 内容(当未解码时)

问题描述 投票:0回答:1

我有一个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: "&lt;strong&gt;&lt;span style=&quot;font-size: 20px;&quot;&gt;M&lt;/span&gt;&lt;/strong&gt;&lt;span style=&quot;color: rgb(247, 150, 70);&quot;&gt;e&lt;/span&gt;mo&lt;span style=&quot;font-size: 24px; font-family: Verdana;&quot;&gt;&lt;em&gt;6&lt;/em&gt;&lt;/span&gt;"

它应该以适当的格式显示 html 内容,但我不确定为什么它以解码格式显示,如下图所示:

Screenshot

提前感谢您的帮助!

根据我的测试,MyHtml 对象中的绑定内容在其编码的 html 格式中保持正确,但由于某种原因,在绑定时显示以编码格式显示

html .net webview maui maui-community-toolkit
1个回答
0
投票

如果你想在代码后面设置Html字符串,你可以做一些改变。

注:

C# 中的

&lt;
应该等同于 C# 中的
<

C# 中的

&gt;
应该等同于 C# 中的
>

C# 中的

&quot;
应该等同于 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("&lt;","<").Replace("&gt;",">").Replace("&quot;",""");

更多信息,您可以参考显示本地HTML

© www.soinside.com 2019 - 2024. All rights reserved.