の暗黙的なスタイル Xamarin.Forms

サンプルのダウンロードサンプルのダウンロード

暗黙的なスタイルは、各コントロールがスタイルを参照する必要なく、同じ TargetType のすべてのコントロールで使用されるスタイルです。

XAML で暗黙的なスタイルを作成する

ページ レベルで を Style 宣言するには、 ResourceDictionary をページに追加してから、 に 1 つ以上 Style の宣言を ResourceDictionary含めることができます。 Styleは、属性を指定しないことによって暗黙的x:Key行われます。 その後、スタイルは、 と完全に一致 TargetType するビジュアル要素に適用されますが、値から TargetType 派生した要素には適用されません。

次のコード例は、ページの で XAML で宣言され、ページのResourceDictionaryインスタンスに適用される暗黙的なスタイルを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のインスタンスに適用される 1 つの暗黙的なスタイルを定義します。 Styleは、黄色の背景に青いテキストを表示し、他の外観オプションも設定するために使用されます。 Styleは、属性を指定せずにページの ResourceDictionaryx:Key追加されます。 したがって、 Style は、 の Entry プロパティStyleと完全に一致するため、TargetTypeすべてのインスタンスに暗黙的に適用されます。 ただし、 Style はサブクラス化された Entryインスタンスには適用CustomEntryされません。 この結果、次のスクリーンショットに示すような外観が表示されます。

暗黙的なスタイルの例

さらに、4 番目Entryの は、暗黙的なスタイルの プロパティと TextColor プロパティを異なるColor値にオーバーライドBackgroundColorします。

コントロール レベルで暗黙的なスタイルを作成する

ページ レベルで 暗黙的な スタイルを作成するだけでなく、次のコード例に示すように、コントロール レベルで作成することもできます。

<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な がコントロールのStackLayoutコレクションにResources割り当てられます。 その後、 暗黙的な スタイルをコントロールとその子に適用できます。

アプリケーション ResourceDictionaryの でスタイルを作成する方法については、「 グローバル スタイル」を参照してください。

C で暗黙的なスタイルを作成する#

Styleインスタンスは、次のResourcesコード例に示すように、新しい ResourceDictionaryを作成し、 インスタンスを にResourceDictionary追加することで、C# のページのコレクションに追加Styleできます。

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のインスタンスに適用される 1 つの暗黙的なスタイルを定義します。 Styleは、黄色の背景に青いテキストを表示し、他の外観オプションも設定するために使用されます。 Styleは、文字列を指定せずにページの ResourceDictionarykey追加されます。 したがって、 Style は、 の Entry プロパティStyleと完全に一致するため、TargetTypeすべてのインスタンスに暗黙的に適用されます。 ただし、 Style はサブクラス化された Entryインスタンスには適用CustomEntryされません。

派生型にスタイルを適用する

Style.ApplyToDerivedTypesプロパティを使用すると、 プロパティによって参照される基本型から派生したコントロールにスタイルをTargetType適用できます。 したがって、このプロパティを に true 設定すると、 プロパティで指定された基本型から型が派生している場合、1 つのスタイルで複数の型を 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 };