Computer Magic Logo
Implicit styles

Tuesday, April 26, 2016

Published by Aristotelis Pitaridis

Every entry in a ResourceDictionary requires a dictionary key. This is not true when we try to define a style. In that case is generated automatically and all the controls of type defined in the TargetType will be applied the custom style.

<?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="App1.Page1">
  <ContentPage.Resources>
    <ResourceDictionary>
      <Style TargetType="Label">
        <Setter Property="HorizontalOptions" Value="Center" />                 
        <Setter Property="VerticalOptions" Value="CenterAndExpand" />                 
        <Setter Property="TextColor" Value="Red" />                 
        <Setter Property="FontSize" Value="Large" />
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
  <StackLayout>
    <Label x:Name="MyLabel" Text="Hello World!" />
  </StackLayout>
</ContentPage>