How to custom color from global resources?

I want to set my resources on app.xaml and then use it in different versions of the application, but when I set the color, the application will work uu, can anyone help me?

App.xaml

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

use it on stacklayout

    <StackLayout Orientation="Vertical" BackgroundColor="{StaticResource Primary}">
+4
source share
3 answers

Have you called InitializeComponent in App.xaml.cs?

+3
source

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 ResourceDictionaryat the application 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- StaticResource :

 <Label Text="{Binding Text}" TextColor = "{StaticResource textColor}"/>
+2
BackgroundColor="{DynamicResource Primary}"

DynamicResource, StaticResource.

, :

App.xaml

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

and page.xaml has

TextColor="{DynamicResource titleColor}"
+1
source

Source: https://habr.com/ru/post/1672506/


All Articles