GLOBAL STYLES IN PRISM XAMARIN FORMS

How do I use global styles?

Best Answer

Answers

  • VulcanVulcan TWMember ✭✭✭
    You can define Style on App.xaml
  • BrunoPortesBrunoPortes USMember

    Doesn't work @Vulcan
    I'm using prism.

    APP.XAML

    <?xml version="1.0" encoding="utf-8" ?>

    <Application.Resources>
    < ResourceDictionary>
    < Style x:Key="buttonStyle" TargetType="Button">
    < Setter Property="HorizontalOptions" Value="Center" />
    < Setter Property="VerticalOptions" Value="CenterAndExpand" />
    < Setter Property="BorderColor" Value="Lime" />
    < Setter Property="BorderRadius" Value="5" />
    < Setter Property="BorderWidth" Value="5" />
    < Setter Property="WidthRequest" Value="200" />
    < Setter Property="TextColor" Value="Teal" />
    < /Style>
    < /ResourceDictionary>
    < /Application.Resources>
    < /prism:PrismApplication>


    LOGIN.XAML

    <?xml version="1.0" encoding="utf-8" ?>

    <StackLayout 
        Margin="30,0"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="Center">
        <Label TextColor="White" Text="Email" FontAttributes="Bold" FontSize="Medium" />
        <Entry Placeholder="Informe seu email"  />
        <Label TextColor="White" Text="Senha" FontAttributes="Bold" FontSize="Medium" />
        <Entry Placeholder="Informe sua senha" IsPassword="True" />
        <Button 
            TextColor="White" 
            BackgroundColor="#234270" 
            FontAttributes="Bold"
            Text="LOGAR" 
            Command="{Binding NavigateCommand}" 
            Style="{StaticResource buttonStyle}" 
        />
        <Image Source="faceLogin.png" Margin="0,20,0,0" />
    </StackLayout>
    

  • Does your App constructor have a InitializeComponent() call in it?
  • BrunoPortesBrunoPortes USMember

    yes, have Initialize

  • RexHiebertRexHiebert USUniversity

    For Prism Xamarin Forms your App needs to inherit from PrismApplication

    public partial class App : PrismApplication

    Because the inherited base type has changed, this means your App.xaml file also needs to change to:

    <?xml version="1.0" encoding="utf-8"?> <prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms" x:Class="MyMobileApp.App"> <prism:PrismApplication.Resources> <ResourceDictionary> <Style x:Key="MyStackLayoutStyle" TargetType="StackLayout"> <Setter Property="BackgroundColor" Value="#FFFFFF"/> <Setter Property="Orientation" Value="Vertical"/> <Setter Property="VerticalOptions" Value="Fill"/> </Style> ...etc...

  • Plus @RexHeibert answer:

    public partial class App : PrismApplication
    {
        public App(IPlatformInitializer initializer = null)
                   : base(initializer)
        {
        }
    
        protected override void OnInitialized()
        {
            InitializeComponent();
            NavigationService.NavigateAsync("MainPage");
        }
    
        // Some other code .....
    }
    

    I tested it and it worked.

  • SchinwinkwinskySchinwinkwinsky BRMember ✭✭

    On my solution, I´m using Prism and it has a project for any platform: Android, iOS and UWP. But on Android, the global styles don´t work. The Android MainActivity class inherits from FormsAppCompatActivity. I followed the Xamarin docs for AppCompat: https://developer.xamarin.com/guides/xamarin-forms/platform-features/android/appcompat. Could it be because of this?

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @Schinwinkwinsky said:
    On my solution, I´m using Prism and it has a project for any platform: Android, iOS and UWP. But on Android, the global styles don´t work. The Android MainActivity class inherits from FormsAppCompatActivity. I followed the Xamarin docs for AppCompat: https://developer.xamarin.com/guides/xamarin-forms/platform-features/android/appcompat. Could it be because of this?

    The global styles do work in Prism apps, post a repo app of the behaviour your expecting but can't replicate.

  • SchinwinkwinskySchinwinkwinsky BRMember ✭✭

    @NMackay said:

    @Schinwinkwinsky said:
    On my solution, I´m using Prism and it has a project for any platform: Android, iOS and UWP. But on Android, the global styles don´t work. The Android MainActivity class inherits from FormsAppCompatActivity. I followed the Xamarin docs for AppCompat: https://developer.xamarin.com/guides/xamarin-forms/platform-features/android/appcompat. Could it be because of this?

    The global styles do work in Prism apps, post a repo app of the behaviour your expecting but can't replicate.

    Yeah, you are right! I changed the Primary Color on App.xaml and that color changed on Android too. But why I need to have a Resources/values/colors.xml file on Android?

  • NMackayNMackay GBInsider, University ✭✭✭✭✭
    edited November 2017

    @Schinwinkwinsky

    The Droid theme is needed for material design support (Appcompat). All forms templates now default to AppCompat rather than the older FormsApplicationActivity api.

    https://developer.xamarin.com/guides/xamarin-forms/platform-features/android/appcompat/

    Like the App.xaml file in UWP and specifying Appearance values for controls in iOS, the theme allows you to tweak some of the platform specific defaults. The truth is, you need to be aware of these platform specific approaches for styling to add a little polish to your apps in my experience.

Sign In or Register to comment.