中的隱含樣式 Xamarin.Forms

隱含樣式是相同 TargetType 的所有控件所使用的樣式,而不需要每個控件參考樣式。

在 XAML 中建立隱含樣式

若要在頁面層級宣告 StyleResourceDictionary 必須將 加入至頁面,然後一或多個 Style 宣告可以包含在 中 ResourceDictionaryStyle透過未指定x:Key屬性,以隱含方式建立 。 然後,樣式會套用至符合 TargetType 的視覺專案,但不會套用至衍生自 值的專案 TargetType

下列程式代碼範例會顯示在頁面 的 ResourceDictionaryXAML 中宣告的隱含樣式,並套用至頁面的Entry實例:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Styles;assembly=Styles" x:Class="Styles.ImplicitStylesPage" Title="Implicit" IconImageSource="xaml.png">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="Entry">
                <Setter Property="HorizontalOptions" Value="Fill" />
                <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                <Setter Property="BackgroundColor" Value="Yellow" />
                <Setter Property="FontAttributes" Value="Italic" />
                <Setter Property="TextColor" Value="Blue" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <Entry Text="These entries" />
            <Entry Text="are demonstrating" />
            <Entry Text="implicit styles," />
            <Entry Text="and an implicit style override" BackgroundColor="Lime" TextColor="Red" />
            <local:CustomEntry Text="Subclassed Entry is not receiving the style" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

ResourceDictionary會定義套用至頁面Entry實例的單一隱含樣式。 Style用來在黃色背景上顯示藍色文字,同時設定其他外觀選項。 在 Style 未指定屬性的情況下,會將 新增至頁面 ResourceDictionaryx:Key 。 因此, Style 會隱含地套用至所有 Entry 實例,因為它們 TargetType 完全符合 的 Style 屬性。 不過, Style 不會套用至 CustomEntry 實體,這是子類別化 Entry。 這會導致下列螢幕擷取畫面中顯示的外觀:

隱含樣式範例

此外,第四 Entry 個會 BackgroundColor 覆寫隱含樣式的 和 TextColor 屬性至不同的 Color 值。

在控件層級建立隱含樣式

除了在頁面層級建立 隱含 樣式之外,也可以在控件層級建立它們,如下列程式代碼範例所示:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Styles;assembly=Styles" x:Class="Styles.ImplicitStylesPage" Title="Implicit" IconImageSource="xaml.png">
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <StackLayout.Resources>
                <ResourceDictionary>
                    <Style TargetType="Entry">
                        <Setter Property="HorizontalOptions" Value="Fill" />
                        ...
                    </Style>
                </ResourceDictionary>
            </StackLayout.Resources>
            <Entry Text="These entries" />
            ...
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

在此範例中 ,隱含Style 會指派給 Resources 控件的 StackLayout 集合。 然後,隱含樣式可以套用至控件及其子系。

如需在應用程式 ResourceDictionary中建立樣式的詳細資訊,請參閱 全域樣式

在 C 中建立隱含樣式#

Style實例可以藉由建立新的 ResourceDictionary,然後將實例新增StyleResourceDictionary,以新增至 C# 中的Resources頁面集合,如下列程式代碼範例所示:

public class ImplicitStylesPageCS : ContentPage
{
    public ImplicitStylesPageCS ()
    {
        var entryStyle = new Style (typeof(Entry)) {
            Setters = {
                ...
                new Setter { Property = Entry.TextColorProperty, Value = Color.Blue }
            }
        };

        ...
        Resources = new ResourceDictionary ();
        Resources.Add (entryStyle);

        Content = new StackLayout {
            Children = {
                new Entry { Text = "These entries" },
                new Entry { Text = "are demonstrating" },
                new Entry { Text = "implicit styles," },
                new Entry { Text = "and an implicit style override", BackgroundColor = Color.Lime, TextColor = Color.Red },
                new CustomEntry  { Text = "Subclassed Entry is not receiving the style" }
            }
        };
    }
}

建構函式會定義套用至頁面Entry實例的單一隱含樣式。 Style用來在黃色背景上顯示藍色文字,同時設定其他外觀選項。 Style會加入至頁面的 ResourceDictionary ,而不指定key字串。 因此, Style 會隱含地套用至所有 Entry 實例,因為它們 TargetType 完全符合 的 Style 屬性。 不過, Style 不會套用至 CustomEntry 實體,這是子類別化 Entry

將樣式套用至衍生類型

屬性 Style.ApplyToDerivedTypes 可讓樣式套用至衍生自 屬性所參考基底型別的 TargetType 控件。 因此,將此屬性設定為 true 可讓單一樣式以多個類型為目標,前提是型別衍生自 屬性中指定的 TargetType 基底型別。

下列範例顯示將實例的背景色彩設定為紅色的 Button 隱含樣式:

<Style TargetType="Button"
       ApplyToDerivedTypes="True">
    <Setter Property="BackgroundColor"
            Value="Red" />
</Style>

將此樣式放在頁面層級 ResourceDictionary 會導致它套用至頁面上的所有 Button 實例,也套用至衍生自 Button的任何控件。 不過,如果 ApplyToDerivedTypes 屬性保持未設定,則樣式只會套用至 Button 實例。

對等的 C# 程式碼為:

var buttonStyle = new Style(typeof(Button))
{
    ApplyToDerivedTypes = true,
    Setters =
    {
        new Setter
        {
            Property = VisualElement.BackgroundColorProperty,
            Value = Color.Red
        }
    }
};

Resources = new ResourceDictionary { buttonStyle };