0

How can I dynamically connect Material icon code with prefix \u in Xamarin? This not work:

string iconCode = "e87e";
Label label = new Label
{FontFamily = "IconMaterial", Text = $"\u{iconCode}"}

And How can I do the same thing in XAML code? Thank you!

Beny
  • 1

1 Answers1

0

If you want to use the Material icon, you need to set the FontFamily first.

    <ContentPage.Resources>
    <OnPlatform x:Key="Material" x:TypeArguments="x:String">
        <On Platform="iOS" Value="Material Design Icons" />
        <On Platform="Android" Value="materialdesignicons-webfont.ttf#Material Design Icons" />
    </OnPlatform>

    <Style x:Key="MaterialIcons" TargetType="{x:Type Label}">
        <Setter Property="FontFamily" Value="{DynamicResource Material}" />
        <Setter Property="FontSize" Value="400" />
        <Setter Property="HorizontalOptions" Value="Center" />
        <Setter Property="VerticalOptions" Value="Center" />
        <Setter Property="FontSize" Value="Large" />
    </Style>
</ContentPage.Resources>

<ContentPage.Content>
    <StackLayout>
        <Label x:Name="label"  ></Label>
    </StackLayout>
</ContentPage.Content>

And set the Text in code behind.

 label.Text = "\ue87e";
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17