【问题标题】:How to convert and display html from a json file to xaml with xamarin.forms如何使用 xamarin.forms 将 html 从 json 文件转换并显示为 xaml
【发布时间】:2022-01-04 21:41:22
【问题描述】:

我正在使用带有 xamarin 表单的 C# 来构建一个 Android 应用程序。我对 xaml 进行了硬编码,但可以从 SDK 访问 html。将此 html 显示为 xaml 的最佳方式是什么?

我在下面的对象中存储了一个json文件的字符串值

dynamic dynamicObject = JsonConvert.DeserializeObject(data.ToString()); 运行时Console.WriteLine(dynamicObject.mainText) 其中mainText 是 json 属性并将其值记录为 html

<div style="font-family:sans-serif;">
  <p> some text </p>
  <p> some more text </p>
  <p><a href = "google.com">here's google<a/>
  </p>
</div>

除了将 html 元素的值单独绑定到我们的 xaml 文件元素之外,有没有办法将所有代码解析/转换为 xaml,或者如何显示 html 的最佳方式?

【问题讨论】:

  • 您可以在 webview 或 Label 中显示 HTML

标签: c# html xaml xamarin.forms


【解决方案1】:

网页视图

创建一个HtmlWebViewSource

WebView webview = new WebView();
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = dynamicObject.mainText;
webview.Source = htmlSource;

标签

将 TextType 设置为 Html

Label label = new Label();
label.Text = dynamicObject.mainText;
label.TextType = TextType.Html;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 2022-01-07
    • 2021-10-09
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2016-03-08
    • 2011-02-28
    相关资源
    最近更新 更多