Mohamed Samsudeen
Mohamed Samsudeen

Reputation: 1154

List<Color> in Xaml

I have my List defined in Xaml like this.

  <ContentPage.Resources>
    <ResourceDictionary>
      <local:FileName x:Key="fileName">
        <Color>#3599B8</Color>
        <Color>#374649</Color>
        <Color>#FD625E</Color>
        <Color>#F2C80F</Color>
      </local:FileName> 
    </ResourceDictionary>
  </ContentPage.Resources>

FileName is defined in code behind like this.

public class FileName : List<Color>
    {

    }

Instead of directly setting the Color values, i want to define it as resource like this

<Color x:Key="BasicColorSchemeBlue">#3599B8</Color>

and use it.

Any suggestions on how to do this.

Thanks in advance.

Upvotes: 3

Views: 731

Answers (2)

M. Hamza Rajput
M. Hamza Rajput

Reputation: 10246

GradientColors is an array of Colors.

 <?xml version="1.0" encoding="utf-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                 xmlns:local="clr-namespace:GradientTest"
                 xmlns:s="clr-namespace:System;assembly=mscorlib"
                 x:Class="GradientTest.GradientTestPage">
        <StackLayout Padding="20, 40, 20, 20">
            <local:GradientViewRender HorizontalOptions="Center" 
                                      WidthRequest="300" 
                                      HeightRequest="50" 
                                      x:Name="gradientView">

                <local:GradientViewRender.GradientColors>
                    <x:Array Type="{x:Type Color}"> 
                        <Color>#5FC900</Color>
                        <Color>#0FF2C8</Color> 
                    </x:Array>
                </local:GradientViewRender.GradientColors>
            </local:GradientViewRender>
        </StackLayout>
    </ContentPage>

Upvotes: 2

Jaejatae
Jaejatae

Reputation: 153

I have done like this on a working App. This goes inside App.xaml:

<Color x:Key="COLOR_NAME">#ffffff</Color>

to access the color from a .cs file, use:

(Color)ResourceFinder.FindResource("COLOR_NAME");

Or use StaticResource or DynamicResource within an xaml file.

Upvotes: 0

Related Questions