Xamarin:StaticResource:'FontSize'是否没有属性,可绑定属性或事件?

时间:2018-09-07 13:42:04

标签: xaml xamarin

所以我试图根据本教程设计一个小的登录页面: Xamarin Tutorial

我的代码略有不同,这是我的LoginPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.ViewModels.LoginPage"
             BackgroundColor="{StaticResource BackgroundColor}">
    <ScrollView>
        <Grid RowSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="AUTO"/>
                <RowDefinition Height="AUTO"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <!--header spacing-->
            <BoxView BackgroundColor="Green" Grid.Row="0"/>
            <Image Source="LoginBackground.png" Aspect="AspectFill" HeightRequest="150"/>
            <Image Source="CurvedLimiter.png" VerticalOptions="End" HeightRequest="50" Aspect="Fill"/>

            <!--header-->
            <BoxView BackgroundColor="White" Grid.Row="1" HeightRequest="100"/>
            <StackLayout Grid.Row="1">
                <Entry Text="Email" x:Name="Email" Style="{StaticResource LoginEntry}"/>
                <Entry IsPassword="True" Text="Password" x:Name="Password" Style="{StaticResource LoginEntry}"/>
                <Button Text="Login" x:Name="btnLogin" Clicked="btnLogin_Clicked" Style="{StaticResource LoginButton}"/>
            </StackLayout>

            <!--login-->
            <BoxView BackgroundColor="White" Grid.Row="2"/>
        </Grid>
    </ScrollView>
</ContentPage>

如您所见,我正在使用StaticRessources,这些信息在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="App.App">
    <Application.Resources>
        <ResourceDictionary>
            <!-- colors -->
            <Color x:Key="HeaderTextColor">#585858</Color>
            <Color x:Key="BodyTextColor">#C3C3C3</Color>
            <Color x:Key="ButtonBackgroundColor">#5992FF</Color>
            <Color x:Key="BackgroundColor">#5992FF</Color>

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

            <!-- Font sizes -->
            <Font x:Key="BodyFont" FontSize="18" FontFamily="{StaticResource RegularFontFamily}" />
            <Font x:Key="TagTextFont" FontSize="18" FontFamily="{StaticResource RegularFontFamily}" />
            <Font x:Key="StatsNumberFont" FontSize="20" FontFamily="{StaticResource LightFontFamily}" />
            <Font x:Key="StatsCaptionFont" FontSize="16" FontFamily="{StaticResource LightFontFamily}" />
            <Font x:Key="ButtonFont" FontSize="14" FontFamily="{StaticResource MediumFontFamily}" /> 

            <!-- Styles -->
            <Style x:Key="LoginEntry" TargetType="Entry">
                <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
                <Setter Property="HorizontalTextAlignment" Value="Center"/>
            </Style>

            <Style x:Key="LoginButton" TargetType="Button">
                <Setter Property="BackgroundColor" Value="{StaticResource ButtonBackgroundColor}"/>
                <Setter Property="TextColor" Value="White"/>
                <Setter Property="HeightRequest" Value="40"/>
                <Setter Property="BorderRadius" Value="15"/>
                <Setter Property="Font" Value="{StaticResource ButtonFont}"/>
                <Setter Property="WidthRequest" Value="200"/>
                <Setter Property="HorizontalOptions" Value="Center"/>
            </Style>

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

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

            <Style x:Key="StatsNumberLabel" TargetType="Label">
                <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
                <Setter Property="HorizontalTextAlignment" Value="Center"/>
                <Setter Property="Font" Value="{StaticResource StatsNumberFont}" />
            </Style>

            <Style x:Key="StatsCaptionLabel" TargetType="Label">
                <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
                <Setter Property="Margin" Value="0,-5,0,0"/>
                <Setter Property="HorizontalTextAlignment" Value="Center"/>
                <Setter Property="Font" Value="{StaticResource StatsCaptionFont}" />
            </Style>

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

            <Style x:Key="FollowButton" TargetType="Button">
                <Setter Property="BackgroundColor" Value="{StaticResource ButtonBackgroundColor}"/>
                <Setter Property="TextColor" Value="White"/>
                <Setter Property="HeightRequest" Value="40"/>
                <Setter Property="BorderRadius" Value="20"/>
                <Setter Property="Font" Value="{StaticResource ButtonFont}"/>
            </Style>
        </ResourceDictionary>

    </Application.Resources>
</Application>

问题是,这至少在XAML预览中有效。但是,当我想编译代码时,收到以下错误消息,该错误消息指向第一个Font x:key = ... Line:

  

位置19:36。找不到以下属性,可绑定属性或事件:   “ FontSize”,或值和属性之间的类型不匹配。

为什么?现在XAML预览器也不再起作用,因为该项目无法编译...

非常感谢!

1 个答案:

答案 0 :(得分:0)

您声明的字体错误。

            <Style x:Key="BodyFont" TargetType="Label">
             <Setter Property="FontSize" Value="18"/>
             <Setter Property="FontFamily" Value="{StaticResource RegularFontFamily}"/>
            </Style>

并对其余字体执行相同的操作。