【问题标题】:How to build string for IconCode in Xamarin如何在 Xamarin 中为 IconCode 构建字符串
【发布时间】:2021-11-06 17:38:18
【问题描述】:

如何在 Xamarin 中动态连接带有前缀 \u 的 Material 图标代码? 这不起作用:

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

我怎样才能在 XAML 代码中做同样的事情? 谢谢!

【问题讨论】:

    标签: xaml xamarin icons google-material-icons


    【解决方案1】:

    如果要使用Material icon,需要先设置FontFamily。

        <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>
    

    并在后面的代码中设置文本。

     label.Text = "\ue87e";
    

    【讨论】:

      猜你喜欢
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多