【问题标题】:Binding Fontawesome icons to a dynamic list in Xamarin将 Fontawesome 图标绑定到 Xamarin 中的动态列表
【发布时间】:2022-01-20 16:15:47
【问题描述】:

我有一个需要动态绑定到 Xamarin 页面的图标列表。

Xaml 是:

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

其中 Features 是一个逗号分隔的 Fontawesome 图标列表。硬编码的十六进制值有效

&#xf236;  &#xf1eb;

Unicode 值只是呈现为

"\uf236  \uf1eb"

如何获取要呈现为完整列表的图标列表?

好吧,这真的很奇怪。

这行得通

 string features = "\uf236  \uf1eb \uf540  \uf2e7  \uf2cc \uf084 \uf26c \uf001 \ue065 \uf206";
FeaturesList = features;

这不是

FeaturesList = model.Features;

其中Features list是页面上的绑定列表。 Model.features 包含与字符串特征完全相同的值。感谢大家的帮助

【问题讨论】:

  • Features 中究竟包含什么?请举个具体的例子
  • 字符串值“\uf0c2”

标签: c# xaml xamarin.forms data-binding font-awesome


【解决方案1】:

根据您的陈述

其中 Features 是一个逗号分隔的 Fontawesome 图标列表。硬编码的十六进制值有效

我将假设Features 的以下代码解释,您将需要:

  • 拆分每个图标的代码并删除多余的空格。
  • 将分割图标的代码分配给 List&lt;string&gt; 属性,以便绑定到 UI。
string Features = "\uf236, \uf1eb, \uf540, \uf2e7, \uf2cc, \uf084, \uf26c, \uf001, \ue065, \uf206";
public List<string> IconsList { get; set; }

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

在您的 UI 中,您可以使用 BindableLayoutCollectionViewListView 来绑定 IconsList 属性:

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

注意

如果您正在添加/删除(不仅是在页面首次出现期间),那么您可能需要将类型 List&lt;string&gt; 更改为 ObservableCollection&lt;string&gt;

这个答案不包括如何设置 fonteawesome 图标,看看How to use Font Awesome icons in project as an icon of ImageButton

文档

【讨论】:

  • 您应该能够在单个标签中显示多个字体图标,而无需将它们拆分为列表
  • 是的,但是我不确定 OP 到底需要什么,这只是一个假设,也许他需要使用一个标签,或者他在每个图标之前/之后还有其他东西要添加!?
  • @amun1000 请确保在您的标签上指定FontFamily,同时确保您的嵌入字体设置正确,您可以查看我提供的链接
【解决方案2】:

您也可以使用单个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 用法

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

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

后面的代码

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

参考

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

【讨论】:

  • 感谢这两种解决方案,但似乎都不起作用。仍然为 BindableLayout 显示硬编码字符,并且自定义控件 - 标签上没有文本。有上述的可行解决方案吗? - 谢谢
  • 您是否按照我提供的链接显示字体真棒?
  • 测试一个普通的 Label 看它是否显示像this ?
  • 是的 - 所有人都跟上了。顺便说一句,当我将十六进制更改为对显示的图标进行 unicode 时。但是,当我将它们绑定到模型“string FeatureIcons = model.Features;”时使用 unicode 值的值有 \\ 需要剥离,但我不能只是为了清除使用可绑定布局显示的图标
  • 你能显示model.Features的值吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
  • 1970-01-01
相关资源
最近更新 更多