Static (Centralized) Resources

Hi

I'm trying to create some application wise styles, defined in App.xaml.

I've used this guide to create the App.XAML

In App.Xaml I've added my style:

<Application.Resources>

Pink

</Application.Resources>

Then I've added a new content page and made it my MainPage.

In the XAML of my newly created MainPage I've added a label in order for using my style:


But when running it I get an exception "StaticResource not found for key textColor"

What am I doing wrong. Can any one please make a simple POC project for me?

I have attatched my demo project.

Posts

  • Gigex42Gigex42 USMember ✭✭✭✭
    edited July 2016

    Well just writing pink in there wont do the job :smile:

    You have to define what type of resource it is, the name and value.

    <Application.Resources> <ResourceDictionary> <Color x:Key="NAME">VALUE</Color> </ResourceDictionary> </Application.Resources>

    In your page you can access it by

    <Grid BackgroundColor={StaticResource NAME}>

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/application/

  • Hi Daniel

    I forgot to format my code as Code, sot the side ate most of it. But that was exactly what I did. And still I get the exception.

  • Gigex42Gigex42 USMember ✭✭✭✭
    edited July 2016

    Could you post your code again from you page and app.xaml?

  • App.xaml

        <?xml version="1.0" encoding="utf-8" ?>
        <Application xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     x:Class="App22.App">
    
          <Application.Resources>
            <ResourceDictionary>      
              <Style x:Key="CustomStyle" TargetType="Label" BaseResourceKey="SubtitleStyle">
                <Setter Property="Label.TextColor" Value="Color.Pink"/>
              </Style>
            </ResourceDictionary>
          </Application.Resources>
    
        </Application>
    

    App.xaml.cs

    using Xamarin.Forms;

    namespace App22
    {
        public partial class App : Application
        {
            public App()
            {
                MainPage = new Page1();
            }
        }
    }
    

    page1.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="App22.Page1">
    
      <StackLayout Padding="0,20,0,0">
        <Label Text="TESTING 1 - 2- - 3 - 4 " FontSize="Medium" FontAttributes = "Bold" Style= "{StaticResource CustomStyle}" />
      </StackLayout>
    </ContentPage> 
    

    I've been Googeling a lot and some says that I have to call InitializeComponent(); in App.xaml.cs but that won't compile.

  • Gigex42Gigex42 USMember ✭✭✭✭

    public App() { InitializeComponent(); MainPage = new NavigationPage(new LoginPage()); }

    And in your app.xaml you write
    <Setter Property="Label.TextColor" Value="Color.Pink"/>

    You can delete the 'Label.' and 'Color.'

  • GOT IT!!!!
    x:Class="..." was wrong s it wasn't linked together with it's cs file

    THANK YOU for your time

Sign In or Register to comment.