Xamarin.Form中如何使用Style

在Xamarin.Form中,我们可以这样定义界面的风格;

<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:ForStyle"
    x:Class="ForStyle.ForStylePage">
    <StackLayout>
        <Button
            Text=" Carpe diem "
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand"
            BorderWidth="3"
            TextColor="Red"
            FontSize="Large"/>
        <Button
            Text=" Sapere aude "
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand"
            BorderWidth="3"
            TextColor="Red"
            FontSize="Large"/>
        <Button
            Text=" Discere faciendo "
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand"
            BorderWidth="3"
            TextColor="Red"
            FontSize="Large"/>
    </StackLayout>
</ContentPage>

下面是在Android平台和iOS平台的运行效果,至于WinPhone上的运行效果,我手头既没有WinPhone的真机也没有模拟器,就不给出来了。

Android平台的运行效果
iOS平台的运行效果

上面的代码的有个很明显的问题,三个按键的风格都是一样的,但是却都要写三遍,既冗长,又不便于修改。这个时候,我们可以使用Style来解决这个问题;

<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:ForStyle"
    x:Class="ForStyle.ForStylePage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style
                x:Key="buttonStyle"
                TargetType="Button">
                <Setter
                    Property="HorizontalOptions"
                    Value="Center" />
                <Setter
                    Property="VerticalOptions"
                    Value="CenterAndExpand" />
                <Setter
                    Property="BorderWidth"
                    Value="3" />
                <Setter
                    Property="TextColor"
                    Value="Red" />
                <Setter
                    Property="FontSize"
                    Value="Large" />
            
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout>
        <Button
            Text=" Carpe diem "
            Style="{StaticResource buttonStyle}" />
        <Button
            Text=" Sapere aude "
            Style="{StaticResource buttonStyle}" />
        <Button
            Text=" Discere faciendo "
            Style="{StaticResource buttonStyle}" />
    </StackLayout>
</ContentPage>

注意:

  1. 这里我们要将Style的定义放在<ContentPage.Resources><ResourceDictionary>...</ResourceDictionary></ContentPage.Resources>之中;
  2. x:Key="buttonStyle"指定Style的变量名;
  3. TargetType="Button"指定Style应用的控件类型,这里就是Button;
  4. Style="{StaticResource buttonStyle}"声明对该Style的引用。

其实,我们还可以有更简洁的写法,就是将x:Key="buttonStyle"Style="{StaticResource buttonStyle}"都去掉,没有变量名的Style会默认应用到其将指定的控件类型上,即会默认将这个Button的Style应用到页面中的所有Button控件上。

<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:ForStyle"
    x:Class="ForStyle.ForStylePage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style
                TargetType="Button">
                <Setter
                    Property="HorizontalOptions"
                    Value="Center" />
                <Setter
                    Property="VerticalOptions"
                    Value="CenterAndExpand" />
                <Setter
                    Property="BorderWidth"
                    Value="3" />
                <Setter
                    Property="TextColor"
                    Value="Red" />
                <Setter
                    Property="FontSize"
                    Value="Large" />
            
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout>
        <Button
            Text=" Carpe diem " />
        <Button
            Text=" Sapere aude " />
        <Button
            Text=" Discere faciendo " />
    </StackLayout>
</ContentPage>

但是这个Style目前只能在这个页面中使用,如果我们在整个App中使用这个Style呢?在Xamarin.Form中有个App.xaml文件,我们需要把将Style放在这个文件中。

<?xml version="1.0" encoding="utf-8"?>
<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="ForStyle.App">
    <Application.Resources>
        <!-- Application resource dictionary -->
        <ResourceDictionary>
            <Style
                x:Key="buttonStyle"
                TargetType="Button">
                <Setter
                    Property="HorizontalOptions"
                    Value="Center" />
                <Setter
                    Property="VerticalOptions"
                    Value="CenterAndExpand" />
                <Setter
                    Property="BorderWidth"
                    Value="3" />
                <Setter
                    Property="TextColor"
                    Value="Red" />
                <Setter
                    Property="FontSize"
                    Value="Large" />
            
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在这里x:Key="buttonStyle"是不可避免的,因为在App中定义的Style不会自动应用到程序中的控件,不定义这个变量名,我们就没办法在其它页面中引用到这个Style。此外,我们还需要在具体页面中做一些处理;

<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:ForStyle"
    x:Class="ForStyle.ForStylePage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style
                TargetType="Button"
                BasedOn="{StaticResource buttonStyle}" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout>
        <Button
            Text=" Carpe diem " />
        <Button
            Text=" Sapere aude " />
        <Button
            Text=" Discere faciendo " />
    </StackLayout>
</ContentPage>

BasedOn="{StaticResource buttonStyle}"引用App.xaml文件中定义的buttonStyle之后,页面中的Button都会应用到这个Style。这个时候,页面的代码就会显得简洁许多,我们的关注点就可以放在控件的特性上。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,050评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,023评论 19 139
  • 早上一大早,赶着给人送工资卡,我天,100来公里,感觉,稍微有些远,但杯子一贯比较务实,诚恳,热忱,不怕困难,一旦...
    杯里水阅读 1,373评论 0 0
  • 有时候,我在想是不是真的存在天才。他们不花费什么力气,就能够轻而易举的做到任何事情。 带着这个疑惑,我重新翻看了年...
    黑熊爱折腾阅读 4,574评论 0 3
  • 时间已经僵持很久了,没想到连家的人倒有几分本事。 以剑拄地的黑袍男子欣赏地看着对面战立在尸体堆中的对手——连家堡堡...
    廊亭风阅读 3,017评论 0 1

友情链接更多精彩内容