Computer Magic Logo
Static Resource

Tuesday, April 26, 2016

Published by Aristotelis Pitaridis

Every class that derives from VisualElement has a Resource property of type ResourceDictionary which can be used in order common values for different object properties.

<?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>
      <Color x:Key="textColor">Blue</Color>
      <Color x:Key="backgroundColor" x:FactoryMethod="FromHsla">
        <x:Arguments>
          <x:Double>0</x:Double>
          <x:Double>1</x:Double>
          <x:Double>0.5</x:Double>
          <x:Double>1</x:Double>
        </x:Arguments>
      </Color>
      <x:Double x:Key="rotation">45</x:Double>
    </ResourceDictionary>
  </ContentPage.Resources>
  <Label x:Name="MyLabel" 
         Text="Hello World!" 
         TextColor="{StaticResource Key=textColor}" 
         BackgroundColor="{StaticResource Key=backgroundColor}" 
         Rotation="{StaticResource Key=rotation}" />
</ContentPage>