【问题标题】:Use font awesome icon for tabbed page tabs为标签页标签使用字体真棒图标
【发布时间】:2021-04-24 07:28:09
【问题描述】:

这就是我将标签添加到标签页的方式。 我在我的应用程序的各个地方都有很棒的字体图标。我如何在标签中添加图标,因为我在后面的代码中添加了标签,如下所示:

public partial class MainTabbedPage : TabbedPage
{
    public MainTabbedPage(SelectedItem item)
    {
        InitializeComponent();
        var summaryPage = new DeviceSummaryPage(item);

        Children.Add(summaryPage);
        Children.Add(new DeviceSettingsPage(item));
        Children.Add(new FirmwarePage(item));       
    }
}

【问题讨论】:

    标签: xamarin.forms tabs icons font-awesome tabbedpage


    【解决方案1】:

    如何在标签中添加图标

    首先,您需要将字体复制到特定于平台的项目中:

    1. 在 Android 上使用 Build Action AndroidAsset 进入 Assets 文件夹
    2. 在 iOS 上使用 Build Action BundleResource 进入 Resources 文件夹
    3. 在 UWP 上使用 Build Action Content 进入 Assets 文件夹

    注意:在 iOS 上,需要更改 Info.plist 文件,以便也可以使用添加的字体。

    png 在 Android 平台上。

    为了能够使用字体,请在 App.xaml 中创建 ResourceDictionary

    <ResourceDictionary>
            <OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String">
                <On Platform="Android" Value="FontAwesome5Brands.otf#Regular" />
                <On Platform="iOS" Value="FontAwesome5Brands-Regular" />
                <On Platform="UWP" Value="/Assets/FontAwesome5Brands.otf#Font Awesome 5 Brands" />
            </OnPlatform>
    
            <OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String">
                <On Platform="Android" Value="FontAwesome5Solid.otf#Regular" />
                <On Platform="iOS" Value="FontAwesome5Free-Solid" />
                <On Platform="UWP" Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
            </OnPlatform>
    
            <OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String">
                <On Platform="Android" Value="FontAwesome5Regular.otf#Regular" />
                <On Platform="iOS" Value="FontAwesome5Free-Regular" />
                <On Platform="UWP" Value="/Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
            </OnPlatform>
        </ResourceDictionary>
    

    创建一个 FontImageSource 对象并相应地设置值,然后将此对象传递给 ContentPage 的 IconImageSource。

      public TabbedPage1()
        {
            InitializeComponent();
            var solidFontFamily = Application.Current.Resources["FontAwesomeSolid"] as OnPlatform<string>;
            var RegularFontFamily = Application.Current.Resources["FontAwesomeRegular"] as OnPlatform<string>;
    
    
            Children.Add(new simplecontrol.Page1() { IconImageSource=new FontImageSource() { FontFamily = RegularFontFamily, Glyph = "\uf108" } });
            Children.Add(new simplecontrol.Page3() { IconImageSource = new FontImageSource() { FontFamily = solidFontFamily, Glyph = "\uf108" } });
    
        }
    

    【讨论】:

    • 谢谢,我只是在找这部分 `Children.Add(new simplecontrol.Page1() { IconImageSource=new FontImageSource() { FontFamily = RegularFontFamily, Glyph = "\uf108" } }) ;`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2013-08-05
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2020-09-11
    • 2013-12-07
    相关资源
    最近更新 更多