Xamarin.Forms 之 样式Styles

Xamarin.Forms 版本1.3以下是没有样式的。这里的样式其实跟写html中的样式是一个意思。在Xamarin中一般的元素都具有Style这个属性。

以下是样式种类及设置样式的方式:

C#代码中:

1、自带的样式:Device.Styles.

var mylabel = new Label {
    Text = "This uses TitleStyle",
    Style = Device.Styles.TitleStyle
};

2、自定义样式

var buttonStyle = new Style (typeof(Button)) {
    Setters = {
        new Setter {Property = Button.BackgroundColorProperty, Value = Color.Yellow},
        new Setter {Property = Button.BorderRadiusProperty, Value = 0},
        new Setter {Property = Button.HeightRequestProperty, Value = 42}
    }
}
// apply directly to a single control
var mybutton = new Button {
    Text = "Style Me",
    Style = buttonStyle
};


3、样式也是可以继承的:BaseResourceKey

var customLabel = new Label {
    Text = "This uses a custom style inherited dynamically from SubtitleStyle",
    Style = new Style (typeof(Label)) {
        BaseResourceKey = Device.Styles.SubtitleStyleKey,
        Setters = {
            new Setter {Property = Label.TextColorProperty, Value = Color.Pink}
        }
    }
}
4、全局样式,分两种,一种是根据key,一种是默认全部(没有设置key的时候)。

所有页面上的BoxView有具有该样式的显示

Application.Current.Resources = new ResourceDictionary ();
var boxStyle = new Style (typeof(BoxView)) {
    Setters = {
        new Setter { Property = BoxView.ColorProperty, Value = Color.Aqua }
    }
};
// no Key specified, becomes an implicit style for ALL boxviews
Application.Current.Resources.Add (boxStyle);

所有页面上的Label都可以设置该样式AppStayle,

public class App : Application
{
    public App ()
    {
        // The Application ResourceDictionary is available in Xamarin.Forms 1.3 and later
        Application.Current.Resources = new ResourceDictionary ();
        var appStyle = new Style (typeof(Label)) {
            BaseResourceKey = Device.Styles.SubtitleStyleKey,
            Setters = {
                new Setter { Property = Label.TextColorProperty, Value = Color.Green }
            }
        };
        Application.Current.Resources.Add ("AppStyle", appStyle); // use the "AppStyle" key in the app

        MainPage = new YourContentPage();
    }
}


xaml中的写法:

1 自带样式 DynamicResource

<Label Text="This uses TitleStyle in XAML"
       Style="{DynamicResource TitleStyle}"/>
<Label Text="This uses SubtitleStyle in XAML"
       Style="{DynamicResource SubtitleStyle}"/>
2、自定义样式

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WorkingWithStyles.StyleXaml">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="ButtonStyle" TargetType="Button">
                <Setter Property="BackgroundColor" Value="Yellow"/>
                <Setter Property="BorderRadius" Value="0"/>
                <Setter Property="HeightRequest" Value="42"/>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <Button Text="Style Me XAML" Style="{StaticResource ButtonStyle}" />

</ContentPage>
3、全局样式,这里需要将app.cs文件改写成xaml形式

<ResourceDictionary>
    <Style TargetType="BoxView">
        <Setter Property="Color" Value="Aqua" />
    </Style>
</ResourceDictionary>

<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="WorkingWithAppResources.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="labelStyle" TargetType="Label">
                <Setter Property="TextColor" Value="Green" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值