使用FontSize的Xaml StaticResource绑定样式崩溃

时间:2017-12-14 14:28:16

标签: c# xaml xamarin xamarin.forms

尝试初始化此内容页面时,我的代码崩溃了。一切正常,除了静态资源,我已经尝试取消注释其余部分,这只是静态资源的一个问题。只有注释掉的代码不起作用。

我不使用OnPlatform要求

<?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="ASFT.IssueManager.LoginPage" Padding="10">
        <ContentPage.Resources>
            <ResourceDictionary>
          <x:String x:Key="Labelfont">Medium</x:String>
          <x:String x:Key="Titlefont">Large</x:String>
        </ResourceDictionary>
      </ContentPage.Resources>
      <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="FillAndExpand" Spacing="10" >
        <StackLayout Orientation="Vertical" VerticalOptions="Start" HorizontalOptions="Center" Spacing="10" WidthRequest="350">
          <Label Text="Login" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Titlefont}"/>
          <BoxView HeightRequest="5" Color="Gray"/>
          <!--<Label Text="Host" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
          <Entry Placeholder="Host/URL" Text="{Binding Host}" />
          <Label Text="UserName" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
          <Entry Placeholder="User name / Account" Text="{Binding Username}" />
          <Label Text="Password" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
          <Entry Placeholder="Password" IsPassword="true" Text="{Binding Password}" />
          <BoxView HeightRequest="5" Color="Gray"/>-->
          <Button x:Name="btnLogin" Text="Login" HorizontalOptions="FillAndExpand" Clicked="OnButtonLogin" WidthRequest="100"/>
        </StackLayout>
      </StackLayout>
    </ContentPage>

1 个答案:

答案 0 :(得分:1)

您可以使用Style通过NamedSize枚举设置FontSize。

将ResourceDictionary更改为使用样式:

<ResourceDictionary>
    <Style x:Key="Labelfont" TargetType="Label">
        <Setter Property="FontSize" Value="Small" />
    </Style>
    <Style x:Key="Titlefont" TargetType="Label">
        <Setter Property="FontSize" Value="Large" />
    </Style>
</ResourceDictionary>

更改您的控件以引用样式:

<StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="FillAndExpand" Spacing="10" >
    <StackLayout Orientation="Vertical" VerticalOptions="Start" HorizontalOptions="Center" Spacing="10" WidthRequest="350">
          <Label Text="Login" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Titlefont}"/>
          <BoxView HeightRequest="5" Color="Gray"/>
          <Label Text="Host" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Labelfont}"/>
          <Entry Placeholder="Host/URL"  />
          <Label Text="UserName" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Labelfont}"/>
          <Entry Placeholder="User name / Account"  />
          <Label Text="Password" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Labelfont}"/>
          <Entry Placeholder="Password" IsPassword="true"  />
          <BoxView HeightRequest="5" Color="Gray"/>
          <Button x:Name="btnLogin" Text="Login" HorizontalOptions="FillAndExpand" WidthRequest="100"/>
    </StackLayout>
</StackLayout>

输出:

enter image description here