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

时间:2017-07-10 03:01:42

标签: xamarin xamarin.forms

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

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

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

3 个答案:

答案 0 :(得分:13)

也许,您可以在 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
    }
    ...
}
  

如果您想在C#中定义资源

Error: Undefined class constant 'MYSQL_ATTR_SSL_CA' in Drush\Sql\Sqlmysql->creds() (line 56 of phar:///usr/local/bin/drush/lib/Drush/Sql/Sqlmysql.php).

答案 1 :(得分: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
    }
}

答案 2 :(得分:3)

要在C#中创建样式,请参阅此link to a xamarin developer guide for global styles

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

您可以在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"]
                }
            }
        };
    }
}

或者在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>