简体   繁体   English

使用Xamarin Forms图标化(在Android上)

[英]Iconize with Xamarin Forms (on Android)

I have not been able to get Iconize to work at all. 我根本无法让Iconize正常工作。 I created a small test project based on the code in this thread (among other places): 我根据此线程中的代码(以及其他地方)创建了一个小型测试项目:

How to change icon color in Xamarin.Forms xaml page? 如何在Xamarin.Forms xaml页面中更改图标颜色?

In App.xaml.cs: 在App.xaml.cs中:

public App()
{
    InitializeComponent();

    Plugin.Iconize.Iconize.With(new Plugin.Iconize.Fonts.MaterialDesignIconsModule());
    MainPage = new MainPage();
}

In MainActivity.cs: 在MainActivity.cs中:

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

    Plugin.Iconize.Iconize.Init(Resource.Id.toolbar, Resource.Id.sliding_tabs);

    LoadApplication(new App());
}

and MainPage.xaml: 和MainPage.xaml:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:IconizeTest"
             xmlns:iconize="clr-namespace:Plugin.Iconize;assembly=Plugin.Iconize"
             x:Class="IconizeTest.MainPage">
    <StackLayout>
        <StackLayout Orientation="Horizontal">
            <iconize:IconImage HeightRequest="20" Icon="mdi-bus-double-decker" IconColor="Blue" WidthRequest="20" />
            <iconize:IconImage HeightRequest="20" Icon="mdi-bus-double-decker" BackgroundColor="Blue" IconColor="Yellow" WidthRequest="20" IconSize="10" />
            <iconize:IconButton FontSize="20" Text="mdi-bus-double-decker" TextColor="Red" WidthRequest="48" />
            <iconize:IconLabel FontSize="20" Text="mdi-bus-double-decker" TextColor="Green" VerticalTextAlignment="Center" />
            <Label Text="mdi-bus-double-decker" VerticalTextAlignment="Center" />
        </StackLayout>
    </StackLayout>
</ContentPage>

Here's a picture of what the output looks like in an emulator (looks the same on my test device...): 这是仿真器中输出的图片(在我的测试设备上看起来是一样的...):

在此处输入图片说明

I like the idea of using iconize, it will tremendously reduce the amount of time I take dorking with icons. 我喜欢使用iconize的想法,它将极大地减少我花时间浏览图标的时间。 I just can't get it to work. 我就是无法正常工作。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

Thanks! 谢谢! -Karen -Karen

Font icons can be used directly in Label . 字体图标可以直接在Label

Image and ImageButton supports defining font icons in FontImageSource , see example using Iconize Material Design Icons below: ImageImageButton支持在FontImageSource定义字体图标 ,请参见下面的使用Iconize Material Design Icons的示例:

<ContentPage ...
             xmlns:fonts="clr-namespace:Fonts" 
             ...
             >
<ContentPage.Resources>
  <ResourceDictionary>
    <OnPlatform x:Key="MaterialDesignIcons" x:TypeArguments="x:String">
        <On Platform="iOS" Value="Material Design Icons" />
        <On Platform="Android" Value="iconize-materialdesignicons.ttf#Material Design Icons" />
        <On Platform="UWP" Value="Assets/Fonts/iconize-materialdesignicons.ttf#Material Design Icons" />
    </OnPlatform>
  </ResourceDictionary>
</ContentPage.Resources>
...
<Image BackgroundColor="LightGray">
    <Image.Source>
        <FontImageSource Glyph="{x:Static fonts:MaterialDesign.access_point}"
                         FontFamily="{StaticResource MaterialDesignIcons}" 
                         Color="Blue" Size="10" />
    </Image.Source>
</Image>
<ImageButton BackgroundColor="LightGray">
    <ImageButton.Source>
        <FontImageSource Glyph="{x:Static fonts:MaterialDesign.bus_double_decker}"
                         FontFamily="{StaticResource MaterialDesignIcons}" 
                         Color="Red" Size="20" />
    </ImageButton.Source>
</ImageButton>
<Label Text="{x:Static fonts:MaterialDesign.lock}" 
       FontFamily="{StaticResource MaterialDesignIcons}" 
       TextColor="Green" FontSize="20" BackgroundColor="LightGray" VerticalTextAlignment="Center" />

The font icons can be defined as follows, example from Iconize fonts in Iconize/src/Fonts/Plugin.Iconize.MaterialDesignIcons/MaterialDesignIconsCollection.cs ; 字体图标可以定义如下,例如从Iconize/src/Fonts/Plugin.Iconize.MaterialDesignIcons/MaterialDesignIconsCollection.cs Iconize字体示例;

Icons.Add("mdi-access-point", '\uf002');
...
Icons.Add("mdi-bus-double-decker", '\uf79d');
...
Icons.Add("mdi-lock", '\uf33e');
...

So the class MaterialDesign below is placed in the same assembly as the ContentPage would be: 因此,下面的类MaterialDesignContentPage放置在同一程序集中:

namespace Fonts
{
    public static class MaterialDesign
    {
#pragma warning disable IDE1006 // Naming Styles
        public static string access_point => "\uf002";
        ...
        public static string bus_double_decker => "\uf79d";
        ...
        //lock is a keyword in c#, so add "@" to use it as name of property
        public static string @lock => "\uf33e";
        ...
#pragma warning restore IDE1006 // Naming Styles
    }
}

The key,value entries of the Iconize MaterialDesignIconsCollection class can be converted to the auto property syntax of the MaterialDesign class using eg search/replace in a text editor (not shown here). 该键,图标化的值项MaterialDesignIconsCollection类可转换成的汽车属性的语法MaterialDesign使用如搜索类/替换在文本编辑器(这里没有显示)。

In this example the iconize-materialdesignicons.ttf font file is placed in the Assets folder on Android (build action AndroidAsset ), Resources folder on iOS (build action BundleResource ), and Assets/Fonts on UWP (build action Content ). 在此示例中,将iconize-materialdesignicons.ttf字体文件放置在Android上的Assets文件夹(构建动作AndroidAsset ),iOS上的Resources文件夹(构建动作BundleResource )和UWP上的Assets/Fonts (构建动作Content )中。

On iOS add the font to Info.plist in the <app>.iOS project: 在iOS上,将字体添加到<app>.iOS项目中的Info.plist中:

<key>UIAppFonts</key>
<array>
    <string>iconize-materialdesignicons.ttf</string>
</array>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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