【问题标题】:Xamarin: StaticResource: No property, bindable property or event for 'FontSize'?Xamarin:StaticResource:“FontSize”没有属性、可绑定属性或事件?
【发布时间】:2018-09-07 13:42:04
【问题描述】:

所以我正在尝试根据本教程设计一个小的登录页面: 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 预览器也不再工作了,因为项目无法编译...

提前非常感谢!

【问题讨论】:

  • 很确定这应该是 x:TypeArguments="x:Double" Value="18 "
  • 感谢您的提示:当我编写以下内容时: 编译器只是告诉我“值”不是字体的属性。此外,FontSize 属性至少在 XAML 预览器中有效..
  • 补充:我刚刚看了一下 Font 结构本身。它确实包含一个“FontSize”属性。
  • 附加2:如果我从
  • 那是因为你做错了。您应该以与您完成按钮类似的方式进行操作。我会写在下面的答案中。

标签: xaml xamarin


【解决方案1】:

您声明您的字体错误。

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

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

【讨论】:

  • 感谢您的回答,听起来不错,将其 1:1 复制到我的 app.xaml 中,但它不起作用。在编译时我收到以下错误:“Can't resolve Fontsize on Font”..?
  • 对不起,它应该是标签而不是字体。
  • 谢谢,但是这似乎与我最初尝试做的不同。我想定义一个通用字体,然后可以在不同的 TargetTypes 中使用。
猜你喜欢
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多