在Xamarin.Form中,我們可以這樣定義界面的風格;
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ForStyle"
x:Class="ForStyle.ForStylePage">
<StackLayout>
<Button
Text=" Carpe diem "
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
BorderWidth="3"
TextColor="Red"
FontSize="Large"/>
<Button
Text=" Sapere aude "
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
BorderWidth="3"
TextColor="Red"
FontSize="Large"/>
<Button
Text=" Discere faciendo "
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
BorderWidth="3"
TextColor="Red"
FontSize="Large"/>
</StackLayout>
</ContentPage>
下面是在Android平臺和iOS平臺的運行效果,至于WinPhone上的運行效果,我手頭既沒有WinPhone的真機也沒有模擬器,就不給出來了。
Android平臺的運行效果
iOS平臺的運行效果
上面的代碼的有個很明顯的問題,三個按鍵的風格都是一樣的,但是卻都要寫三遍,既冗長,又不便于修改。這個時候,我們可以使用Style來解決這個問題;
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ForStyle"
x:Class="ForStyle.ForStylePage">
<ContentPage.Resources>
<ResourceDictionary>
<Style
x:Key="buttonStyle"
TargetType="Button">
<Setter
Property="HorizontalOptions"
Value="Center" />
<Setter
Property="VerticalOptions"
Value="CenterAndExpand" />
<Setter
Property="BorderWidth"
Value="3" />
<Setter
Property="TextColor"
Value="Red" />
<Setter
Property="FontSize"
Value="Large" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Button
Text=" Carpe diem "
Style="{StaticResource buttonStyle}" />
<Button
Text=" Sapere aude "
Style="{StaticResource buttonStyle}" />
<Button
Text=" Discere faciendo "
Style="{StaticResource buttonStyle}" />
</StackLayout>
</ContentPage>
注意:
- 這里我們要將Style的定義放在
<ContentPage.Resources><ResourceDictionary>...</ResourceDictionary></ContentPage.Resources>
之中; - 用
x:Key="buttonStyle"
指定Style的變量名; - 用
TargetType="Button"
指定Style應用的控件類型,這里就是Button; - 用
Style="{StaticResource buttonStyle}"
聲明對該Style的引用。
其實,我們還可以有更簡潔的寫法,就是將x:Key="buttonStyle"
和Style="{StaticResource buttonStyle}"
都去掉,沒有變量名的Style會默認應用到其將指定的控件類型上,即會默認將這個Button的Style應用到頁面中的所有Button控件上。
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ForStyle"
x:Class="ForStyle.ForStylePage">
<ContentPage.Resources>
<ResourceDictionary>
<Style
TargetType="Button">
<Setter
Property="HorizontalOptions"
Value="Center" />
<Setter
Property="VerticalOptions"
Value="CenterAndExpand" />
<Setter
Property="BorderWidth"
Value="3" />
<Setter
Property="TextColor"
Value="Red" />
<Setter
Property="FontSize"
Value="Large" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Button
Text=" Carpe diem " />
<Button
Text=" Sapere aude " />
<Button
Text=" Discere faciendo " />
</StackLayout>
</ContentPage>
但是這個Style目前只能在這個頁面中使用,如果我們在整個App中使用這個Style呢?在Xamarin.Form中有個App.xaml文件,我們需要把將Style放在這個文件中。
<?xml version="1.0" encoding="utf-8"?>
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ForStyle.App">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<Style
x:Key="buttonStyle"
TargetType="Button">
<Setter
Property="HorizontalOptions"
Value="Center" />
<Setter
Property="VerticalOptions"
Value="CenterAndExpand" />
<Setter
Property="BorderWidth"
Value="3" />
<Setter
Property="TextColor"
Value="Red" />
<Setter
Property="FontSize"
Value="Large" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
在這里x:Key="buttonStyle"
是不可避免的,因為在App中定義的Style不會自動應用到程序中的控件,不定義這個變量名,我們就沒辦法在其它頁面中引用到這個Style。此外,我們還需要在具體頁面中做一些處理;
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ForStyle"
x:Class="ForStyle.ForStylePage">
<ContentPage.Resources>
<ResourceDictionary>
<Style
TargetType="Button"
BasedOn="{StaticResource buttonStyle}" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Button
Text=" Carpe diem " />
<Button
Text=" Sapere aude " />
<Button
Text=" Discere faciendo " />
</StackLayout>
</ContentPage>
用BasedOn="{StaticResource buttonStyle}"
引用App.xaml文件中定義的buttonStyle之后,頁面中的Button都會應用到這個Style。這個時候,頁面的代碼就會顯得簡潔許多,我們的關注點就可以放在控件的特性上。