有没有办法可以自定义我经常使用的标签?

Ala*_*an2 13 xamarin xamarin.forms

在我的Xamarin XAML中,我多次使用它:

<Label Text="{x:Static local:FontAwesome.FACheck}" FontFamily="FontAwesome" 
   XAlign="Center" FontSize="13" 
   TextColor="#1E90FF" />
Run Code Online (Sandbox Code Playgroud)

有没有办法使用C#我可以创建自定义版本的Label并使用它,所以我不必继续指定字体和其他东西?

Jes*_*ulo 13

也许,您可以StyleApp.xaml中使用a .

<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>
Run Code Online (Sandbox Code Playgroud)

然后在您的页面中,您只需在任何需要放置此标签的地方使用它.

<Label Style="{StaticResource FACheck}"/>
Run Code Online (Sandbox Code Playgroud)

如果您想在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
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)


Yur*_*i S 6

创建您的课程并在任何地方使用它

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


Dig*_*1nt 3

要在 C# 中创建样式,请参阅此指向全局样式的 xamarin 开发人员指南的链接

C# 代码示例:(在您的 App 类中设置资源字典)

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);
        ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

您可以在 C# 类中创建具有以下样式的控件:

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"]
                }
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

或者在 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>
Run Code Online (Sandbox Code Playgroud)