13

In my Xamarin XAML I use this many times:

<Label Text="{x:Static local:FontAwesome.FACheck}" FontFamily="FontAwesome" 
   XAlign="Center" FontSize="13" 
   TextColor="#1E90FF" />

Is there a way using C# that I can create a custom version of Label and use that so I don't have to keep specifying the Font and other things?

3 Answers 3

13

Maybe, you can use a Style in your App.xaml.

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourAPp.App">
    <Application.Resources>
       <ResourceDictionary>
            <Style x:Key="FACheck" TargetType="Label">
                <Setter Property="Text" Value="{x:Static local:FontAwesome.FACheck}"/>
                <Setter Property="FontFamily" Value="FontAwesome"/>
                <Setter Property="XAlign" Value="Center"/>
                <Setter Property="FontSize" Value="13"/>
                <Setter Property="TextColor" Value="#1E90FF"/>
            </Style>
       <ResourceDictionary>
    </Application.Resources>
</Application>

And then in your pages, you just need to use it wherever you need to place this label.

<Label Style="{StaticResource FACheck}"/>

In case you want to define your resources in C#

public class App : Application
{
    public App ()
    {

            //Begin - Style code

            var faCheckStyle = new Style(typeof(Label))
            {
                Setters = {
                    new Setter { Property = Label.TextProperty,   Value = FontAwesome.FAChcek },
                    new Setter { Property = Label.FontFamilyProperty, Value = "FontAwesome" },
                    new Setter { Property = Label.XAlignProperty, Value = "FontAwesome" },
                    new Setter { Property = Label.FontSizeProperty, Value = 13 },
                    new Setter { Property = Label.TextColorProperty, Value = Color.FromHex("#1E90FF") }
                 }
            };
            Resources = new ResourceDictionary();
            Resources.Add("FACheck", faCheckStyle);      

            //End Style code
    }
    ...
}
3
  • 1
    Thanks Jesus, your suggestion looks interesting. I upvoted but didn't yet click on accept. Could I also create this resource in C#?
    – Alan2
    Jul 10, 2017 at 3:26
  • Yeah, you can create this in C#, remember that XAML is just syntac sugar over C# code. Jul 10, 2017 at 13:13
  • @Alan you can use code instead :) . I updated my answer Jul 14, 2017 at 13:49
6
+50

Create your class and use it everywhere

public class MyAwesomeLabel : Xamarin.Forms.Label
{
    public MyAwesomeLabel()
    {
        FontFamily = "FontAwesome";
        XAlign = Xamarin.Forms.TextAlignment.Center; //deprecated BTW
        FontSize = 13;
        TextColor = Color.FromHex(0x1E90FF);
        //etc
    }
}
3

To create a style in C# please see this link to a xamarin developer guide for global styles.

Example of the C# code: (sets up a resource dictionary in your App class)

public class App : Application
{
    public App ()
    {
        var buttonStyle = new Style (typeof(Button)) {
            Setters = {
                ...
                new Setter { Property = Button.TextColorProperty,   Value = Color.Teal }
                new Setter { Property = Button.BackgroundColor,   Value = Color.White }

                // add more setters for the properties that you want to set here
            }
        };

        // add this style into your resource dictionary.
        Resources = new ResourceDictionary ();
        Resources.Add ("buttonStyle", buttonStyle);
        ...
    }
    ...
}

You can create controls with these styles in your C# classes:

public class ApplicationStylesPageCS : ContentPage
{
    public ApplicationStylesPageCS ()
    {
        ...
        Content = new StackLayout {
            Children = {
                new Button { Text = "These buttons", Style = (Style)Application.Current.Resources ["buttonStyle"] },
                new Button { Text = "are demonstrating", Style = (Style)Application.Current.Resources ["buttonStyle"] },
                new Button { Text = "application styles", Style = (Style)Application.Current.Resources ["buttonStyle"]
                }
            }
        };
    }
}

Or alternatively access it in the xaml as a static resource:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" Icon="xaml.png">
       <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <Button Text="These buttons" Style="{StaticResource buttonStyle}" />
            <Button Text="are demonstrating" Style="{StaticResource buttonStyle}" />
            <Button Text="application style overrides" Style="{StaticResource buttonStyle}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.