Есть ли способ настроить ярлык, который я часто использую?

В моем Xamarin XAML я использую это много раз:

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

Есть ли способ использовать С#, чтобы я мог создать пользовательскую версию Label и использовать это, поэтому мне не нужно указывать шрифт и другие вещи?

Ответ 1

Чтобы создать стиль на С#, см. ссылку в руководстве разработчика xamarin для глобальных стилей.

Пример кода С#: (устанавливает словарь ресурсов в вашем классе приложения)

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);
        ...
    }
    ...
}

Вы можете создавать элементы управления с этими стилями в своих классах С#:

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"]
                }
            }
        };
    }
}

Или, альтернативно, получить доступ к нему в xaml как статическом ресурсе:

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

Ответ 2

Возможно, вы можете использовать Style в 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>

И затем на ваших страницах вам просто нужно использовать его везде, где вам нужно разместить эту метку.

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

Если вы хотите определить свои ресурсы в С#

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

Создайте свой класс и используйте его везде

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