如何将平台上的参数添加到Xamarin标签中?

如何将平台上的参数添加到Xamarin标签中?,xamarin,xamarin.forms,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"  

以下是我的代码:

<?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}" />
但如何将其转换为参数?


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

我不确定我是否正确理解了你的问题,但这就是你想要的吗?

像这样,你可以试试。Xamarin.Forms 3.2中的平台语法发生了变化(我猜是这样)



但是,如果您想使用旧语法(问题中提到的),旧语法也会起作用。

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

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

将此添加到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>
 <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>
<?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>
  <Label x:Name="Register" Text="Registeration"  HorizontalTextAlignment ="Center" FontSize="20"  Style="{StaticResource labelStyle}">
                                    <Label.GestureRecognizers >
                                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" ></TapGestureRecognizer>
                                    </Label.GestureRecognizers>
                                </Label> 
   label.Style = (Style)Application.Current.Resources["labelStyle"];