【问题标题】:How to add fontAwesome icon to toolbar menu如何将 fontAwesome 图标添加到工具栏菜单
【发布时间】:2019-10-25 16:03:25
【问题描述】:
如何向我的工具栏项添加图标(fontAwesome)?参考this link,我设法为我的标签设置了一个字体。但是如何为工具栏 itrm 做到这一点?
我的代码:
ToolbarItem Save = new ToolbarItem();
Save.Text = FontAwesome.FAFloppyO;
Save.Order = ToolbarItemOrder.Primary;
我也尝试将图标属性添加到我的工具栏项,但不起作用。
【问题讨论】:
标签:
c#
xamarin.forms
font-awesome
【解决方案1】:
我已经成功地使用 Iconize library for Xamarin Forms 实现了这个开箱即用的功能。它很容易设置。
现在,为了节省您额外的时间,您需要:
- 使用 IconNavigationPage 作为父级而不是 NavigationPage。
- 使用 IconToolbarItem 代替 ToolbarItem。
- 应该设置 Icon 属性,而不是 Text 属性(即 Icon="fa-user")
如果这能解决您的问题,请告诉我。
【解决方案2】:
您可以使用NavigationPage.TitleView 代替工具栏或工具栏项。
你所要做的只是为每个平台添加fontawesome库,然后根据目标平台设置字体家族属性。
<NavigationPage.TitleView>
<StackLayout Orientation="Horizontal">
<Button HorizontalOptions="EndAndExpand" Text="">
<Button.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="Font Awesome 5 Free" />
<On Platform="Android" Value="fa-solid-900.ttf#fa-solid" />
</OnPlatform>
</Button.FontFamily>
</Button>
</StackLayout>
这是基本用法之一。为了更好地使用,您可以创建自己的static resources 并应用于按钮或其他任何项目。
这是我的一个项目的代码示例:
<!-- Toolbar -->
<NavigationPage.TitleView>
<Grid HeightRequest="50" BackgroundColor="{StaticResource DarkBackgroundColor}" Margin="0" Padding="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- Title -->
<Label Text="Inventory" Style="{StaticResource TitleViewTitleLabel}"/>
<Button Grid.Column="1" Text=""
Style="{StaticResource FontAwesomeButtonRegularTitleViewButton}"
Command="{Binding SyncDataCommand}"/>
<Button Grid.Column="2" Text=""
Style="{StaticResource FontAwesomeButtonRegularTitleViewButton}"
Command="{Binding ShowFunctionalityCommand}"/>
<Button Grid.Column="3" Text=""
Style="{StaticResource FontAwesomeButtonRegularTitleViewButton}"
Command="{Binding AddNewInventoryCommand}"/>
</Grid>
</NavigationPage.TitleView>