Application resources

EnricoRossiniEnricoRossini USMember ✭✭✭✭

Hi guys,
I have a question about Application Resources. I should change the style of my application. I tried to follow the instruction on https://developer.xamarin.com/guides/xamarin-forms/xaml/resource-dictionaries/ but nothing changed in my application.

I removed App.cs and I created a new ContentPage and I changed it like that:

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" 
     xmlns:theme="clr-namespace:Xamarin.Forms.Themes;assembly=Xamarin.Forms.Theme.Light" 
     x:Class="myInventories.App">
    <Application.Resources>
    <ResourceDictionary>
        <Color x:Key="PageBackgroundColor">Yellow</Color>
        <Color x:Key="HeadingTextColor">Black</Color>
        <Color x:Key="NormalTextColor">Blue</Color>
        <Style x:Key="LabelPageHeadingStyle" TargetType="Label">
            <Setter Property="FontAttributes" Value="Bold" />
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="TextColor" Value="{StaticResource HeadingTextColor}" />
        </Style>
        <Style TargetType="Entry">
            <Setter Property="HorizontalOptions" Value="Fill" />
            <Setter Property="VerticalOptions" Value="CenterAndExpand" />
            <Setter Property="BackgroundColor" Value="Yellow" />
            <Setter Property="FontAttributes" Value="Italic" />
            <Setter Property="TextColor" Value="Blue" />
        </Style>
    </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        MainPage = new StartPage();
    }

    public static Page GetMainPage()
    {
        return new RootPage();
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

I was expecting the background application Yellow. What is wrong? Thank you in advance.

Tagged:

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Let's see the markup for your page.
    Did you actually assign the styles and resources to your page like they show on the walk-through? Or did you think just making the resource was enough and that it would 'somehow' apply itself.
    Style="{StaticResource LabelNormalStyle}" Etc.

  • EnricoRossiniEnricoRossini USMember ✭✭✭✭

    I didn't get it. When I define in the App.Xaml in the resourcedictionary section something like

            <Style TargetType="Entry">
                <Setter Property="HorizontalOptions" Value="Fill" />
                <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                <Setter Property="BackgroundColor" Value="Yellow" />
                <Setter Property="FontAttributes" Value="Italic" />
                <Setter Property="TextColor" Value="Blue" />
            </Style>
    

    doesn't it applied automatically to every page?

  • SchinwinkwinskySchinwinkwinsky BRMember ✭✭

    I think it applies automatically, but not for Android.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited November 2017

    @EnricoRossini said:
    I didn't get it. When I define in the App.Xaml in the resourcedictionary section something like

          <Style TargetType="Entry">
              <Setter Property="HorizontalOptions" Value="Fill" />
              <Setter Property="VerticalOptions" Value="CenterAndExpand" />
              <Setter Property="BackgroundColor" Value="Yellow" />
              <Setter Property="FontAttributes" Value="Italic" />
              <Setter Property="TextColor" Value="Blue" />
          </Style>
    

    doesn't it applied automatically to every page?

    No. Just because you define a style doesn't make it apply everywhere. Typically you would give that style a name, then use it on the elements you want. If you want it to apply to every Entry on a page you can put it in the ResourceDictionary for the page.

    If you walk through my site I explain this in more detail in the first few lessons. www.RedPillXamarin.com

  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited June 2018

    @ClintStLaurent

    No. Just because you define a style doesn't make it apply everywhere.

    He is trying to define implicit Global Styles. Doing so should be enough to have it used everywhere that target type is used, yes? [Except where an explicit style is referenced, which would override it.]

    He has the right concept. If it isn't getting applied, there is either something wrong with how he's done it, or it is a xamarin bug.

    @EnricoRossini

    I was expecting the background application Yellow.

    Why are you expecting that?

    1. Read that link about defining implicit styles. [Looks like you already did, for the style you made for Entry.]
    2. Manually write a page with a yellow background.
    3. Think how to convert that single page with yellow background to an implicit style for all pages. What is the target type? [ContentPage]. What is the property name [BackgroundColor I think - I haven't looked]. Now, what is the syntax for defining an implicit style with that target type and that property name?
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @SteveShaw.5557
    Yes. You're right. And that works as expected now in 2018. Back in 2016 when the question and original answer was given it was far less reliable. Named styles worked fairly well then, but the implicit styles not so much. Now they are both good.

  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited June 2018

    @ClintStLaurent - ok. Good to know. Your answer that I was responding to is dated Nov. 2017, hence I was a bit surprised by what you said... was Android still that unreliable? Really glad I skipped X Forms until recently.

Sign In or Register to comment.