の暗黙的なスタイル 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
は、属性を指定せずにページの ResourceDictionary
にx: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
は、文字列を指定せずにページの ResourceDictionary
にkey
追加されます。 したがって、 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 };