Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access color from App.xaml in C# in Xamarin Forms?

Tags:

In App.xaml I have this code:

<Application.Resources>
    <ResourceDictionary>
        <Color x:Key="Yellow">#ffd966</Color>
    </ResourceDictionary>
</Application.Resources>

and in C# I have this code:

public Color BackgroundColor
{
    get { return IsSelected ? Color.Yellow : Color.White; }
}

And I would like to change Color.Yellow with color from App.xaml. How can I reference color from App.xaml in C#?

like image 295
Uros Avatar asked Apr 18 '17 08:04

Uros


People also ask

What is app XAML CS?

xaml. cs is the code-behind page for MainPage. xaml. It's where you add your app logic and event handlers. Together these two files define a new class called MainPage , which inherits from Page, in the HelloWorld namespace.

Is Xamarin the same as XAML?

XAML allows developers to define user interfaces in Xamarin. Forms applications using markup rather than code. XAML is never required in a Xamarin. Forms program, but it is often more succinct and more visually coherent than equivalent code, and potentially toolable.


1 Answers

isSelected ? (Color) Application.Current.Resources["Yellow"] : Color.White;

I think Conversion Color.FromHex() is not needed as you are defining resource as a color. Hope that helps.

like image 145
Dilmah Avatar answered Nov 06 '22 07:11

Dilmah