如何在Xamarin标签中添加平台参数?

时间:2019-02-27 09:52:32

标签: xamarin xamarin.forms

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
    <Label xmlns="http://xamarin.com/schemas/2014/forms" 
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
           xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
           x:Class="Japanese.Templates.MessageLabel" 
           FontSize="{DynamicResource MessageTextFontSize}" 
           TextColor="{DynamicResource FooterTextColor}" />

我要这样做,以使Property="FontFamily"的设置如下:

<OnPlatform x:TypeArguments="x:String">
   <On Platform="iOS" Value="FontAwesome5ProLight" />
   <On Platform="Android" Value="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight" />
</OnPlatform>

但是如何将其设置为参数?

4 个答案:

答案 0 :(得分:2)

像这样,您可以尝试。平台语法在Xamarin.Forms 3.2(我想)中已更改,

<Label xmlns="http://xamarin.com/schemas/2014/forms" 
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
           xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
           x:Class="Japanese.Templates.MessageLabel" 
           FontSize="{DynamicResource MessageTextFontSize}" 
           TextColor="{DynamicResource FooterTextColor}">
           <Label.FontFamily>
                <OnPlatform
                    x:TypeArguments="x:String"
                    Android="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight"
                    iOS="FontAwesome5ProLight" />
            </Label.FontFamily>

</Label>           

但是如果您想使用旧语法(问题中提到的)也可以使用。

答案 1 :(得分:1)

    <Label xmlns="http://xamarin.com/schemas/2014/forms" 
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
           xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
           x:Class="Japanese.Templates.MessageLabel" 
           FontSize="{DynamicResource MessageTextFontSize}" 
           TextColor="{DynamicResource FooterTextColor}">
        <Label.FontFamily>
            <OnPlatform x:TypeArguments="x:String">
               <On Platform="iOS" Value="FontAwesome5ProLight" />
               <On Platform="Android" Value="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight" />
            </OnPlatform>
        </Label.FontFamily>
    </Label>

我不确定我是否正确理解了您的问题,但是那是您要找的东西吗?

答案 2 :(得分:0)

为了更好地使用,请在样式中定义字体系列,并在需要的地方应用

 <OnPlatform x:TypeArguments="x:String" x:Key="MediumFont">
            <On Platform="Android" Value="fonts/HKGrotesk_Medium.ttf#HK Grotesk" />
            <On Platform="UWP" Value="/Assets/Fonts/HKGrotesk_Medium.ttf#HK Grotesk" />
            <On Platform="iOS" Value="HKGrotesk-Medium" />
        </OnPlatform>

答案 3 :(得分:0)

在App.xaml中添加它

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Auction.App">
    <Application.Resources>

        <ResourceDictionary>
            <OnPlatform x:TypeArguments="x:String" 
                        Android="FontAwesome5ProLight.otf#FontAwesome5ProLight" 
                        iOS="FontAwesome5ProLight" 
                        WinPhone="20" 
                        x:Key="FontFamilyName" />


            <Style x:Key="labelStyle" TargetType="Label">
                <Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}"/>
                <Setter Property="FontSize" Value="12" />
                <Setter Property="VerticalTextAlignment" Value="Center"/>
                <Setter Property="TextColor" Value="#000000"/>
            </Style>
         </ResourceDictionary>
    </Application.Resources>
</Application>

在应用的任何页面中使用 在Xaml中

  <Label x:Name="Register" Text="Registeration"  HorizontalTextAlignment ="Center" FontSize="20"  Style="{StaticResource labelStyle}">
                                    <Label.GestureRecognizers >
                                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" ></TapGestureRecognizer>
                                    </Label.GestureRecognizers>
                                </Label> 

和CS

   label.Style = (Style)Application.Current.Resources["labelStyle"];