J.Dhaik
J.Dhaik

Reputation: 103

Xaml StaticResource binding style crash using FontSize

When trying to initalize this contentpage my code crashes. Everything works fine except the static resources, I've tried uncommenting the rest and it's only a problem with the static resource. Only the commented out code doesn't work.

I don't use the OnPlatform requirement

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

Upvotes: 1

Views: 606

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

You can use a Style to set the FontSize via the NamedSize enum.

Change ResourceDictionary to use styles:

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

Change your controls to reference the styles:

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

Output:

enter image description here

Upvotes: 1

Related Questions