Issue
As an Android developer I'm used to work with @dimen/
-constants in Androids XML. I find this future useful because it allows me to easily change multiple places that should have the same pixel-length together.
Does Xamarin.Forms have a similar features that I can use?
Solution
Well what you looking for are ResourceDictionaries
XAML resources are definitions of objects that can be shared and re-used throughout a Xamarin.Forms application. These resource objects are stored in a resource dictionary.
A ResourceDictionary
is a repository for resources that are used by a Xamarin.Forms application. Typical resources that are stored in a ResourceDictionary include styles, control templates, data templates, colours, and converters.
In XAML, resources that are stored in a ResourceDictionary
can then be retrieved and applied to elements by using the StaticResource
markup extension. In C#
, resources can also be defined in a ResourceDictionary
and then retrieved and applied to elements by using a string-based indexer. However, there's little advantage to using a ResourceDictionary
in C#
, as shared objects can simply be stored as fields
or properties
, and accessed directly without having to first retrieve them from a dictionary
.
Creating and Consuming a ResourceDictionary
Resources
are defined in a ResourceDictionary
that is then set to one of the following Resources properties:
- The Resources property of any class that derives from Application.
- The Resources property of any class that derives from VisualElement
A Xamarin.Forms
program contains only one class that derives from Application
but often makes use of many classes that derive from VisualElement
, including pages, layouts, and controls. Any of these objects can have its Resources property set to a ResourceDictionary
. Choosing where to put a particular ResourceDictionary
impact where the resources can be used:
Resources in a
ResourceDictionary
that is attached to a view such asButton
orLabel
can only be applied to that particular object, so this is not very useful.Resources
in aResourceDictionary
attached to a layout such asStackLayout
orGrid
can be applied to the layout and all the children of that layout.Resources in a
ResourceDictionary
defined at the page level can be applied to the page and to all its children.Resources in a
ResourceDictionary
defined at the application level can be applied throughout the application.
The following XAML shows resources defined in an application level ResourceDictionary
in the App.xaml
file created as part of the standard Xamarin.Forms program:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<Color x:Key="PageBackgroundColor">Yellow</Color>
<Color x:Key="HeadingTextColor">Black</Color>
<Color x:Key="NormalTextColor">Blue</Color>
<Style x:Key="LabelPageHeadingStyle" TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="TextColor" Value="{StaticResource HeadingTextColor}" />
</Style>
</ResourceDictionary>
</Application.Resources>
Beginning in Xamarin.Forms
3.0, the explicit ResourceDictionary
tags are not required. The ResourceDictionary
object is created automatically, and you can insert the resources directly between the Resources property-element tags:
<Application ...>
<Application.Resources>
<Color x:Key="PageBackgroundColor">Yellow</Color>
<Color x:Key="HeadingTextColor">Black</Color>
<Color x:Key="NormalTextColor">Blue</Color>
<Style x:Key="LabelPageHeadingStyle" TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="TextColor" Value="{StaticResource HeadingTextColor}" />
</Style>
</Application.Resources>
Each resource has a key that is specified using the x:Key attribute, which becomes it dictionary key in the ResourceDictionary
. The key is used to retrieve a resource from the ResourceDictionary
by the StaticResource
markup extension, as demonstrated in the following XAML code example that shows additional resources defined within the StackLayout
:
<StackLayout Margin="0,20,0,0">
<StackLayout.Resources>
<ResourceDictionary>
<Style x:Key="LabelNormalStyle" TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource NormalTextColor}" />
</Style>
<Style x:Key="MediumBoldText" TargetType="Button">
<Setter Property="FontSize" Value="Medium" />
<Setter Property="FontAttributes" Value="Bold" />
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Label Text="ResourceDictionary Demo" Style="{StaticResource LabelPageHeadingStyle}" />
<Label Text="This app demonstrates consuming resources that have been defined in resource dictionaries."
Margin="10,20,10,0"
Style="{StaticResource LabelNormalStyle}" />
<Button Text="Navigate"
Clicked="OnNavigateButtonClicked"
TextColor="{StaticResource NormalTextColor}"
Margin="0,20,0,0"
HorizontalOptions="Center"
Style="{StaticResource MediumBoldText}" />
</StackLayout>
For more detailed information kindly take a look here
Answered By - FreakyAli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.