官术网_书友最值得收藏!

Using styles

Previously, we created a simple chips container to display the various features of an item that is currently being offered through our application. 

In the previous setup, we were only utilizing the Margin property and VerticalTextAlignment for the labels:

<FlexLayout Direction="Row" Wrap="Wrap">
<Label Text="Feature 1" Margin="4" VerticalTextAlignment="Center" BackgroundColor="Gray" />
<Label Text="Feat. 2" Margin="4" VerticalTextAlignment="Center" BackgroundColor="Lime"/>
<!-- Additional Labels -->
</FlexLayout>
The background property is specific for each feature element, and so we will not be modifying it for now.

Let's modify the look of these items to make the labels look more like chips in order to improve the user experience:

  1. We will start by wrapping up the label in a frame and styling the frame:
 <Frame 
IsVisible="{Binding HasFeature1}"
BackgroundColor="Gray"
CornerRadius="7"
Padding="3"
Margin="4"
HasShadow="false"
>
<Label x:Name="Feat1" Text="Feature 1"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center" />
</Frame>

While this creates a more desirable look, adding these properties to each feature would create a completely redundant XAML structure:

  1. We can now create create two styles (that is, one for the feature label and one for the frame itself) that will be applied to each element, thereby decreasing the redundancy:
<Style TargetType="Frame">
<Setter Property="HasShadow" Value="false" />
</Style>
<Style TargetType="Frame" x:Key="ChipContainer">
<Setter Property="CornerRadius" Value="7" />
<Setter Property="Padding" Value="3" />
<Setter Property="Margin" Value="3" />
</Style>
<Style TargetType="Label" x:Key="ChipLabel">
<Setter Property="VerticalTextAlignment" Value="Center" />
<Setter Property="HorizontalTextAlignment" Value="Center" />
<Setter Property="TextColor" Value="White" />
</Style>
  1. Next, we need to apply these implicit (that is, the HasShadow="false" setter will be applied to all the frames on the application level) and explicit styles (note the x:Key declaration on the ChipContainer and ChipLabel styles):
<FlexLayout Direction="Row" Wrap="Wrap" FlowDirection="LeftToRight" AlignItems="Start">
<Frame IsVisible="{Binding HasFeature1}"
BackgroundColor="Gray" Style="{StaticResource
ChipContainer}"
>
<Label x:Name="Feat1" Text="Feature 1" Style="
{StaticResource ChipLabel}"
/>
</Frame>
<Frame IsVisible="{Binding HasFeature2}" BackgroundColor="Lime"
Style="{StaticResource ChipContainer}">
<Label x:Name="Feat2" Text="Feat. 2" Style="{StaticResource
ChipLabel}" />
</Frame>
<!-- Additional Labels -->
</FlexLayout>

In doing so, we will be decreasing the clutter and redundancy in our XAML tree. Styles can be declared at the application level (as in this case) as global styles using App.xaml. Additionally, they can also be declared at page and view levels using local resource dictionaries. 

Another approach to styling controls would be to use CSS style sheets. While the style sheets currently do not support the full extent of the XAML control styles, can prove powerful, especially when utilizing the CSS selectors. Let's get started:

  1. If we were to recreate the styles for our chip views, the style declarations would be similar to the following:
 .ChipContainerClass {
border-radius: 7;
padding: 3;
margin: 3;
}

.ChipLabelClass {
text-align: center;
vertical-align: central;
color: white;
}

For those of you who are not familiar with CSS, here, we have created two style classes named ChipContainerClass and ChipLabelClass.

  1. Now, we can use these classes with our controls using the StyleClass attribute:
 <Frame IsVisible="{Binding HasFeature1}" 
BackgroundColor="Gray" StyleClass="ChipContainerClass">
<Label x:Name="Feat1" Text="Feature 1"
StyleClass="ChipLabelClass" />
</Frame>
  1. We can take our style declaration one step further and apply the style directly to the child label within the frame with the ChipContainerClass style class (that is, we will remove the style class from the Label element):
 .ChipContainerClass {
border-radius: 7;
padding: 3;
margin: 3;
}

.ChipContainerClass>^label {
text-align: center;
vertical-align: central;
color: white;
}
The difference between .ChipContainerClass>label and .ChipContainerClass>^label is that by using the  ^ (base class) notation, we can make sure that even if we modify the view using a custom control deriving from label, we can make sure that the styles are applied in the same way.

Styles can also be used in conjunction with Xamarin.Forms behaviors to not only modify the visualization, but also the behavior of elements.

主站蜘蛛池模板: 合川市| 枞阳县| 新营市| 儋州市| 庄河市| 门头沟区| 乳源| 二手房| 泗阳县| 英山县| 涪陵区| 孟州市| 开原市| 和平县| 德安县| 合山市| 灌阳县| 克东县| 宜川县| 新邵县| 瑞安市| 札达县| 台州市| 天等县| 个旧市| 凭祥市| 香港 | 阳曲县| 汝州市| 文山县| 噶尔县| 泾源县| 鸡西市| 阿合奇县| 肥西县| 上思县| 南川市| 永泰县| 鹤峰县| 辽阳县| 贞丰县|