locked
Different Style within single XML for Entry, Button RRS feed

  • Question

  • User344985 posted

    Hello,

    I have created Style like below

    <Application.Resources>
        <ResourceDictionary>
         <Style x:Key="EntryStyle" TargetType="Entry">
             <Setter Property="TextColor" Value="Black" />
             <Setter Property="FontSize">
                 <OnPlatform x:TypeArguments="x:String" Android="Medium" iOS="Medium" />
              </Setter>
             <Setter Property="FontAttributes"
                Value="Bold"/>
          </Style>
          </ResourceDictionary>
     </Application.Resources>
    

    and used it for Entry in XML working fine, but now i have button too on page need different Font size and Text Color, so i tried below code

     <Application.Resources>
        <ResourceDictionary>
         <Style x:Key="EntryStyle" TargetType="Entry">
             <Setter Property="TextColor" Value="Black" />
             <Setter Property="FontSize">
                 <OnPlatform x:TypeArguments="x:String" Android="Medium" iOS="Medium" />
              </Setter>
             <Setter Property="FontAttributes"
                Value="Bold"/>
          </Style>
    
             <Style x:Key="ButtonStyle" TargetType="Button">
             <Setter Property="TextColor" Value="White" />
             <Setter Property="FontSize">
                 <OnPlatform x:TypeArguments="x:String" Android="Small" iOS="Small" />
              </Setter>
             <Setter Property="FontAttributes"
                Value="Bold"/>
          </Style>
          </ResourceDictionary>
     </Application.Resources>
    

    But my App is crashing....

    How i can define different Style and used it in XML ? is it possible to keep all Style in one XML file and used it through out App ? or any better way for Shared (iOS & Android) App in Xamarin.forms ?

    Please help.... thanks in advance

    Thursday, January 18, 2018 5:37 PM

Answers

  • User325400 posted

    @Priya2018: Hi Priya,

    instead of string change it to double as shown in below code

                    <Style x:Key="MyLabel" TargetType="Label" BaseResourceKey="TitleStyle">
                        <Setter Property="FontSize" >
                            <Setter.Value>
    
                                <OnIdiom x:TypeArguments="x:Double">
                                    <OnIdiom.Phone>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="14" Android="15"  />
                                    </OnIdiom.Phone>
                                    <OnIdiom.Tablet>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="21" Android="21"  />
                                    </OnIdiom.Tablet>
                                </OnIdiom>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="TextColor" Value="#770754" />
                        <Setter Property="FontAttributes" Value="None" />
                    </Style>
    

    You can place above code in App.xaml page so you can use this style "MyLabel" in all pages in the project for labels

    <Label Text="{Binding name}" Grid.Row="0" Grid.Column="1" TextColor="#770754" Style="{StaticResource MyLabel}" >

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 19, 2018 6:19 PM

All replies

  • User325400 posted

    @Priya2018: Hi Priya,

    instead of string change it to double as shown in below code

                    <Style x:Key="MyLabel" TargetType="Label" BaseResourceKey="TitleStyle">
                        <Setter Property="FontSize" >
                            <Setter.Value>
    
                                <OnIdiom x:TypeArguments="x:Double">
                                    <OnIdiom.Phone>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="14" Android="15"  />
                                    </OnIdiom.Phone>
                                    <OnIdiom.Tablet>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="21" Android="21"  />
                                    </OnIdiom.Tablet>
                                </OnIdiom>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="TextColor" Value="#770754" />
                        <Setter Property="FontAttributes" Value="None" />
                    </Style>
    

    You can place above code in App.xaml page so you can use this style "MyLabel" in all pages in the project for labels

    <Label Text="{Binding name}" Grid.Row="0" Grid.Column="1" TextColor="#770754" Style="{StaticResource MyLabel}" >

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 19, 2018 6:19 PM
  • User344985 posted

    @DarshanJS said:

    @Priya2018: Hi Priya,

    instead of string change it to double as shown in below code

                    <Style x:Key="MyLabel" TargetType="Label" BaseResourceKey="TitleStyle">
                        <Setter Property="FontSize" >
                            <Setter.Value>
    
                                <OnIdiom x:TypeArguments="x:Double">
                                    <OnIdiom.Phone>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="14" Android="15"  />
                                    </OnIdiom.Phone>
                                    <OnIdiom.Tablet>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="21" Android="21"  />
                                    </OnIdiom.Tablet>
                                </OnIdiom>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="TextColor" Value="#770754" />
                        <Setter Property="FontAttributes" Value="None" />
                    </Style>
    

    You can place above code in App.xaml page so you can use this style "MyLabel" in all pages in the project for labels

    <Label Text="{Binding name}" Grid.Row="0" Grid.Column="1" TextColor="#770754" Style="{StaticResource MyLabel}" >

    Hello Darshan,

    Thank you for reply i was just checking to create App.xml since i do not have XML only .CS. i guess i need to add new and used that and delete old one...

    so since i am very new to xamarin is this much better way i can have global of defining Style and using throughout out APP? as i am going through documents i found what you mentioned is one of the way, i will be using.

    just wanted to know if this is the best way as i can stick to it........you can send me link so that i can go through it if some other best way is possible...like mostly followed...

    Tuesday, January 23, 2018 7:33 AM
  • User325400 posted

    @Priya2018 said:

    @DarshanJS said:

    @Priya2018: Hi Priya,

    instead of string change it to double as shown in below code

                    <Style x:Key="MyLabel" TargetType="Label" BaseResourceKey="TitleStyle">
                        <Setter Property="FontSize" >
                            <Setter.Value>
    
                                <OnIdiom x:TypeArguments="x:Double">
                                    <OnIdiom.Phone>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="14" Android="15"  />
                                    </OnIdiom.Phone>
                                    <OnIdiom.Tablet>
                                        <OnPlatform x:TypeArguments="x:Double" iOS="21" Android="21"  />
                                    </OnIdiom.Tablet>
                                </OnIdiom>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="TextColor" Value="#770754" />
                        <Setter Property="FontAttributes" Value="None" />
                    </Style>
    

    You can place above code in App.xaml page so you can use this style "MyLabel" in all pages in the project for labels

    <Label Text="{Binding name}" Grid.Row="0" Grid.Column="1" TextColor="#770754" Style="{StaticResource MyLabel}" >

    Hello Darshan,

    Thank you for reply i was just checking to create App.xml since i do not have XML only .CS. i guess i need to add new and used that and delete old one...

    so since i am very new to xamarin is this much better way i can have global of defining Style and using throughout out APP? as i am going through documents i found what you mentioned is one of the way, i will be using.

    just wanted to know if this is the best way as i can stick to it........you can send me link so that i can go through it if some other best way is possible...like mostly followed...

    Hi Priya,

    You can refer below links, It may help you, https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/application/

    https://blogs.msdn.microsoft.com/devfish/2016/06/24/global-resources-in-xamarin-forms-no-app-xaml-create-one/

    https://www.youtube.com/watch?v=nY3eXlNJDnA

    Tuesday, January 23, 2018 7:39 AM