简体   繁体   中英

Binding Fontawesome icons to a dynamic list in Xamarin

I have a list of icons that need to be dynamically bound to a Xamarin page.

The Xaml is:

<Label
    Grid.Row="2"
    Grid.ColumnSpan="4"
    HorizontalOptions="Start"
    Style="{StaticResource IconLabelStyle}"
    Text="{Binding Features}"/>

where Features is a comma separated list of Fontawesome icons. Hardcoded hex values work

&#xf236;  &#xf1eb;

Unicode values just render as

"\uf236  \uf1eb"

How can I get the list of icons to render as a complete list?

Based on your statement

where Features is a comma separated list of Fontawesome icons. Hardcoded hex values work

I will assume the below code interpretation of Features , you will need to:

  • Split each icon's code and remove extra spaces.
  • Assign divided icon's codes to a List<string> property, in order to bind to the UI.
string Features = "&#xf236;, &#xf1eb, &#xf236;, &#xf1eb";
public List<string> IconsList { get; set; }

    public MainPage()
    {
        BindingContext = this;
        IconsList = Features.Trim().Split(',').Select(x => x.Trim()).ToList();
        InitializeComponent();
    }

In your UI you can use bind IconsList property using BindableLayout , CollectionView or ListView :

<StackLayout BindableLayout.ItemsSource="{Binding IconsList}" Orientation="Horizontal">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Label Text="{Binding .}"/>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Note

If you are adding/removing (not only during first appearing of the page), then you may need to change the type List<string> to ObservableCollection<string> .

Docs

You can also use a single Label to implement it .

Custom Label

public class MyLabel : Label
{
    public static readonly BindableProperty MyTextProperty =
      BindableProperty.Create("MyText", typeof(string), typeof(MyLabel), null, propertyChanged: BindingPropertyChangedDelegate);

    public string MyText
    {
        get { return (string)GetValue(MyTextProperty); }
        set { SetValue(MyTextProperty, value); }
    }

    static void BindingPropertyChangedDelegate(BindableObject bindable, object oldValue, object newValue)
    {
        if (newValue == null) return;

        var IconsList = ((string)newValue).Trim().Split(',').Select(x => x.Trim()).ToList();

        if (IconsList == null || IconsList.Count == 0) return;

        FormattedString text = new FormattedString();

        foreach(var str in IconsList)
        {
            text.Spans.Add(new Span { Text = "  " });
            text.Spans.Add(new Span { Text = str, FontFamily = "FontAwesomeSolid"});
        }

        ((Label)bindable).FormattedText = text;
    }
}

Xaml Usage

xmlns:local="clr-namespace:MyForms.View"

<local:MyLabel MyText="{Binding Features}"/>

Code behind

Features = "\uf164,\uf140,\uf236,\uf1eb,\uf5e1,\uf14a,\uf14b,\uf520,\uf14d,\uf14e,\uf578,\uf15c";
this.BindingContext = this;

在此处输入图像描述

Refer to

https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM