Is there any way I can add HTML into a Xamarin.Forms page?

前端 未结 1 617
有刺的猬
有刺的猬 2021-02-14 13:41

I would like to be able to add some HTML into a Xamarin.Forms page? Is this possible? Also is it possible to add this as part of a label?

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 14:13

    If you want full-blown html support, then Xamarin forms WebView would be way to go. But if you want some basic formatting support in Label as it's native counter-parts (TextView in android, and UILabel in iOS) do, you can implement renderers for that.

    Forms control

    public class HtmlLabel : Label
    {
        public static readonly BindableProperty HtmlProperty =
            BindableProperty.Create(
                "Html", typeof(string), typeof(HtmlLabel),
                defaultValue: default(string));
    
        public string Html
        {
            get { return (string)GetValue(HtmlProperty); }
            set { SetValue(HtmlProperty, value); }
        }
    }
    

    iOS renderer

    [assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))]
    namespace HtmlLabelApp.iOS
    {
        public class HtmlLabelRenderer : LabelRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs

    Android renderer

    [assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))]
    namespace HtmlLabelApp.Droid
    {
        public class HtmlLabelRenderer : LabelRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs

    Sample Usage

    
        
            
            Main Title
                

    A sub-title

    This is some html. Look, here\'s an underline.

    Look, this is emphasized. And here\'s some bold.

    This is a UL list:

    • One
    • Two
    • Three

    This is an OL list:

    1. One
    2. Two
    3. Three
    ]]>

    0 讨论(0)
提交回复
热议问题