Home > other >  How to convert and display html from a json file to xaml with xamarin.forms
How to convert and display html from a json file to xaml with xamarin.forms

Time:12-03

I'm using C# with xamarin forms to build an android app. I hard coded the xaml but can access the html from an SDK. What is the best way to display this html as xaml?

I have stored in the following object, the string value of a json file

dynamic dynamicObject = JsonConvert.DeserializeObject(data.ToString()); when running Console.WriteLine(dynamicObject.mainText) where mainText is the json property and logs it's value as the 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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Instead of individually binding the values of the html elements to our xaml file elements, is there a way to parse/convert all the code to xaml or how is the best way to display the html?

CodePudding user response:

WebView

create a HtmlWebViewSource .

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

Label

set TextType as Html.

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