How to display static HTML string in Xamarin.Forms?
How to display static HTML string in Xamarin.Forms?
To display a static HTML string in Xamarin.Forms, you can use the WebView control along with the HtmlWebViewSource class. Here is a step-by-step guide on how to achieve this:
Create a WebView in your XAML file:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourNamespace.YourPage">
<StackLayout>
<WebView x:Name="webView" HeightRequest="100" WidthRequest="200"/>
</StackLayout>
</ContentPage>
Set the HTML content in the code-behind file:
using Xamarin.Forms;
namespace YourNamespace
{
public partial class YourPage : ContentPage
{
public YourPage()
{
InitializeComponent();
var htmlSource = new HtmlWebViewSource
{
Html = @"<html><body>
<h1>Xamarin.Forms</h1>
<p>Welcome to WebView.</p>
</body></html>"
};
webView.Source = htmlSource;
}
}
}
This example demonstrates how to create a WebView and set its source to an HTML string using the HtmlWebViewSource class. The HTML content is directly embedded within the C# code, making it a static HTML string.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.