Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to user color from global resources?

I want to set my resources on the app.xaml and then use it in the differents views of the app, but when I set the color the app crashes u.u, could someone help me ?

app.xaml

 <Application.Resources>
    <ResourceDictionary>
        <Color x:Key="Primary">#FFC107</Color>
    </ResourceDictionary>
</Application.Resources>

use it in a StackLayout

    <StackLayout Orientation="Vertical" BackgroundColor="{StaticResource Primary}">
like image 871
Alexander Rojas Avatar asked Mar 16 '17 22:03

Alexander Rojas


3 Answers

Has you called InitializeComponent in the App.xaml.cs?

like image 86
farid JAD7 Avatar answered Oct 11 '22 13:10

farid JAD7


you need to use Static Resource,I find a good resource for you:

https://blog.xamarin.com/easy-app-theming-with-xamarin-forms-styles/

So you need to do the following:

1- Define a ResourceDictionary at the app-level in App.xaml

<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="MonkeyTweet.App">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="backgroundColor">#33302E</Color>
            <Color x:Key="textColor">White</Color>
        </ResourceDictionary>
    </Application.Resources>
</Application>

2- Use StaticResource markup extension to reference predefined resources:

 <Label Text="{Binding Text}" TextColor = "{StaticResource textColor}"/>
like image 38
Mohamad Mahmoud Avatar answered Oct 11 '22 13:10

Mohamad Mahmoud


BackgroundColor="{DynamicResource Primary}"

its a DynamicResource, not a StaticResource.

Here's what my code looks like for example:

App.xaml has

<Color x:Key="titleColor">Green</Color>

and a page.xaml has

TextColor="{DynamicResource titleColor}"
like image 30
Allister Avatar answered Oct 11 '22 14:10

Allister