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...

  • mohsensedighimohsensedighi USMember

    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.

Sign In or Register to comment.