我有一個需要動態系結到 Xamarin 頁面的圖示串列。
Xaml 是:
<Label
Grid.Row="2"
Grid.ColumnSpan="4"
HorizontalOptions="Start"
Style="{StaticResource IconLabelStyle}"
Text="{Binding Features}"/>
其中 Features 是一個逗號分隔的 Fontawesome 圖示串列。硬編碼的十六進制值有效
 
Unicode 值只是呈現為
"\uf236 \uf1eb"
如何獲取要呈現為完整串列的圖示串列?
uj5u.com熱心網友回復:
根據你的說法
其中 Features 是一個逗號分隔的 Fontawesome 圖示串列。硬編碼的十六進制值有效
我將假設以下代碼解釋Features,您將需要:
- 拆分每個圖示的代碼并洗掉多余的空格。
- 將分割圖示的代碼分配給
List<string>屬性,以便系結到 UI。
string Features = ", , , ";
public List<string> IconsList { get; set; }
public MainPage()
{
BindingContext = this;
IconsList = Features.Trim().Split(',').Select(x => x.Trim()).ToList();
InitializeComponent();
}
在您的 UI 中,您可以使用系結IconsList屬性BindableLayout,CollectionView或者ListView:
<StackLayout BindableLayout.ItemsSource="{Binding IconsList}" Orientation="Horizontal">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
筆記
如果您正在添加/洗掉(不僅在頁面首次出現期間),那么您可能需要將型別更改List<string>為ObservableCollection<string>.
檔案

參考
https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/415579.html標籤:
