首页 > 解决方案 > 无法分配属性“FontFamily”:属性不存在

问题描述

嗨,Xamarin.forms 的新样式。我有这个旧的 xamarin.forms 应用程序,我进行了调试,但在 App.xaml 中的以下样式出现此错误

问题:位置 39:17。无法分配属性“FontFamily”:属性不存在,或不可分配,或值和属性之间的类型不匹配

请帮忙。

    <!--  Application resource dictionary  -->
    <ResourceDictionary>
        <!--  colors  -->
        <!--<Color x:Key="HeaderTextColor">#585858</Color>
        <Color x:Key="BodyTextColor">#C3C3C3</Color>-->

        <Color x:Key="TimelineColor">#E4B6C3</Color>
        <Color x:Key="HeaderTextColor">#3C3C3C</Color>
        <Color x:Key="BodyTextColor">#869EAC</Color>
        <Color x:Key="TimeColor">#A64C79</Color>

        <!--  font families  -->
        <OnPlatform
            x:Key="RegularFontFamily"
            x:TypeArguments="x:String"
            Android="sans-serif"
            iOS="HelveticaNeue" />
        <OnPlatform
            x:Key="LightFontFamily"
            x:TypeArguments="x:String"
            Android="sans-serif-light"
            iOS="HelveticaNeue-Light" />
        <OnPlatform
            x:Key="MediumFontFamily"
            x:TypeArguments="x:String"
            Android="sans-serif-medium"
            iOS="HelveticaNeue-Medium" />

        <!--  fonts  -->
        <Font
            x:Key="HeaderFont"
            FontFamily="{StaticResource MediumFontFamily}"
            FontSize="30" />
        <Font
            x:Key="SubHeaderFont"
            FontFamily="{StaticResource MediumFontFamily}"
            FontSize="18" />
        <Font
            x:Key="TitleFont"
            FontFamily="{StaticResource MediumFontFamily}"
            FontSize="20" />
        <Font
            x:Key="BodyFont"
            FontFamily="{StaticResource RegularFontFamily}"
            FontSize="18" />


        <!--  styles  -->
        <Style x:Key="PageHeaderLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
            <Setter Property="Font" Value="{StaticResource HeaderFont}" />
        </Style>

        <Style x:Key="SubHeaderLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
            <Setter Property="Font" Value="{StaticResource SubHeaderFont}" />
        </Style>

        <Style x:Key="ClassTimeLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource TimeColor}" />
            <Setter Property="Font" Value="{StaticResource TitleFont}" />
        </Style>

        <Style x:Key="ClassNameLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
            <Setter Property="Font" Value="{StaticResource TitleFont}" />
        </Style>

        <Style x:Key="ClassInstructorLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
            <Setter Property="Font" Value="{StaticResource BodyFont}" />
        </Style>

    </ResourceDictionary>

</Application.Resources>

//--- 更新:我改变了这部分:我仍然有错误

           <ResourceDictionary>

        <Color x:Key="BodyTextColor">#C3C3C3</Color>-->

        <Color x:Key="TimelineColor">#E4B6C3</Color>
        <Color x:Key="HeaderTextColor">#3C3C3C</Color>
        <Color x:Key="BodyTextColor">#869EAC</Color>
        <Color x:Key="TimeColor">#A64C79</Color>

       <OnPlatform
            x:Key="RegularFontFamily"
            x:TypeArguments="x:String"
            Android="sans-serif"
            iOS="HelveticaNeue" />
        <OnPlatform
            x:Key="LightFontFamily"
            x:TypeArguments="x:String"
            Android="sans-serif-light"
            iOS="HelveticaNeue-Light" />
        <OnPlatform
            x:Key="MediumFontFamily"
            x:TypeArguments="x:String"
            Android="sans-serif-medium"
            iOS="HelveticaNeue-Medium" />


        <x:Double x:Key="HeaderFontSize">30</x:Double>
        <x:Double x:Key="SubHeaderFontSize">18</x:Double>
        <x:Double x:Key="BodyFontSize">18</x:Double>
         <x:Double x:Key="TitleFontSize">20</x:Double>



        <Style x:Key="PageHeaderLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
            <Setter Property="Font" Value="{StaticResource HeaderFont}" />
            <Setter Property="FontSize" Value="{StaticResource HeaderFontSize}" />
        </Style>

        <Style x:Key="SubHeaderLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
            <Setter Property="Font" Value="{StaticResource SubHeaderFont}" />
            <Setter Property="FontSize" Value="{StaticResource SubHeaderFontSize}" />
        </Style>

        <Style x:Key="ClassTimeLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource TimeColor}" />
            <Setter Property="Font" Value="{StaticResource TitleFont}" />
            <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" />
        </Style>

        <Style x:Key="ClassNameLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
            <Setter Property="Font" Value="{StaticResource TitleFont}" />
            <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" />
        </Style>

        <Style x:Key="ClassInstructorLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
            <Setter Property="Font" Value="{StaticResource BodyFont}" />
            <Setter Property="FontSize" Value="{StaticResource BodyFontSize}" />
        </Style>

        <Style x:Key="TitleFont" TargetType="Label">
           <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
           <Setter Property="Font" Value="{StaticResource MediumFontFamily}" />
          <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" />
       </Style>

    </ResourceDictionary>

</Application.Resources>

标签: xamarin.forms

解决方案


非常接近,但不是Font用于Double定义FontSize,例如:

<OnPlatform
    x:Key="MediumFontFamily"
    x:TypeArguments="x:String"
    Android="sans-serif-medium"
    iOS="HelveticaNeue-Medium" />

现在您应该定义特定的大小:

<x:Double x:Key="TitleFontSize">20</x:Double>

接下来消费它:

<Style x:Key="TitleFont" TargetType="Label">
    <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
    <Setter Property="FontFamily" Value="{StaticResource MediumFontFamily}" />
    <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" />
</Style>

推荐阅读