Reputation: 24602
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?
Upvotes: 13
Views: 562
Reputation: 5370
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
}
}
Upvotes: 6
Reputation: 2666
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
}
...
}
Upvotes: 13
Reputation: 3276
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>
Upvotes: 3