Xamarin.Forms 中的全局样式

通过将样式添加到应用程序的资源字典中,可以全局提供样式。 这有助于避免跨页面或控件重复样式。

在 XAML 中创建全局样式

默认情况下,从模板创建的所有 Xamarin.Forms 应用程序都使用 App 类来实现 Application 子类。 若要在应用程序级别声明 Style,在使用 XAML 的应用程序的 ResourceDictionary 中,必须使用 XAML App 类和关联的代码隐藏替换默认的 App 类。 有关详细信息,请参阅使用 App 类

以下代码示例演示在应用程序级别声明的 Style

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="buttonStyle" TargetType="Button">
                <Setter Property="HorizontalOptions" Value="Center" />
                <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                <Setter Property="BorderColor" Value="Lime" />
                <Setter Property="BorderRadius" Value="5" />
                <Setter Property="BorderWidth" Value="5" />
                <Setter Property="WidthRequest" Value="200" />
                <Setter Property="TextColor" Value="Teal" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

ResourceDictionary 定义单个显式样式 buttonStyle,该样式将用于设置 Button 实例的外观。 但是,全局样式可以是显式的隐式的

下面的代码示例演示了一个 XAML 页面,该页将 buttonStyle 应用于页面的 Button 实例:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" IconImageSource="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>

这会导致如以下屏幕截图中所示的外观:

全局样式示例

有关在页面的 ResourceDictionary 中创建样式的信息,请参阅“显式样式”和“隐式样式”。

重写样式

视图层次结构中等级较低的样式优先于定义为较高等级的样式。 例如,在应用程序级别设置一个将 Button.TextColor 设置为 RedStyle 时,该操作会被将 Button.TextColor 设置为 Green 的页面级别样式覆盖。 同样,页面级别样式会被控件级别样式覆盖。 此外,如果 Button.TextColor 直接在控件属性上设置,则会优先于任何样式。 以下代码示例演示了这种优先顺序:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" IconImageSource="xaml.png">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="buttonStyle" TargetType="Button">
                ...
                <Setter Property="TextColor" Value="Red" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <StackLayout.Resources>
                <ResourceDictionary>
                    <Style x:Key="buttonStyle" TargetType="Button">
                        ...
                        <Setter Property="TextColor" Value="Blue" />
                    </Style>
                </ResourceDictionary>
            </StackLayout.Resources>
            <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>

在应用程序级别定义的原始 buttonStyle 由在页面级别定义的 buttonStyle 实例重写。 此外,页面级别样式由控件级别 buttonStyle 重写。 因此,Button 实例以蓝色文本显示,如以下屏幕截图所示:

重写样式示例

在 C# 中创建全局样式

可以通过创建新的 ResourceDictionary,然后将 Style 实例添加到 ResourceDictionary,从而将 Style 实例添加到 C# 中的应用程序 Resources 集合,如以下代码示例所示:

public class App : Application
{
    public App ()
    {
        var buttonStyle = new Style (typeof(Button)) {
            Setters = {
                ...
                new Setter { Property = Button.TextColorProperty,    Value = Color.Teal }
            }
        };

        Resources = new ResourceDictionary ();
        Resources.Add ("buttonStyle", buttonStyle);
        ...
    }
    ...
}

构造函数定义单个显式样式,用于在整个应用程序中应用于 Button 实例。 使用 Add 方法将显式Style实例添加到 ResourceDictionary,并指定 key 字符串以引用 Style 实例。 然后,可以将 Style 实例应用于应用程序中正确类型的任何控件。 但是,全局样式可以是显式的隐式的

下面的代码示例显示了将 buttonStyle 应用于页面 Button 实例的 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"]
                }
            }
        };
    }
}

buttonStyle 通过设置 Style 属性来应用于 Button 实例,并会控制 Button 实例的外观。