Windows Phone開發(fā)(15):資源
活字印刷術(shù)是我國(guó)“四大發(fā)明”之一,畢昇在發(fā)明活字印刷術(shù)之后,他很快發(fā)現(xiàn)一個(gè)問題,隨著要印刷資料的不斷增加,要用到的漢字?jǐn)?shù)目越來越多,于是, 他必須尋找一種有效的辦法去管理那些刻有漢字的立方體(暫且就叫立方體,其實(shí)的確是個(gè)立方體),所以,他就和助手們一起努力,為這些立方體進(jìn)行記錄,有標(biāo) 識(shí)地放好,在印刷過程中用到哪些字,就直接取出來,不用了就放回去,既環(huán)保又方便。
這就是資源,水、空氣、陽光也是資源,煤、鐵礦物也是資源,只不過有些可再生,有些***罷了。
何為資源?資源就是客觀存在的,當(dāng)我們需要時(shí)可以拿來利用的一切可支配或可重新組合的東西,如人力資源、人脈資源等。
如果做過網(wǎng)頁,應(yīng)該了解CSS是用來干啥的,其實(shí),我們今天要討論的資源,和CSS樣式表的概念基本一樣,就是把一些經(jīng)常用到的東西保存起來,可以供應(yīng)用 程序中不同地方重復(fù)調(diào)用,這樣我們就不用為每個(gè)控件設(shè)置樣式,我們可以樣式保存到資源列表,用到就取出來,不用重復(fù)定義。
下面看看這段XAML,上面有4個(gè)TextBlock,我現(xiàn)在希望每個(gè)TextBlock的字體字號(hào)為37.5,當(dāng)然,簡(jiǎn)單的值可以方便設(shè)置,如果值很復(fù)雜,如上一篇文章說的模板,那你就很痛苦了,要為每個(gè)控件做一個(gè)模板。
- <StackPanel Orientation="Vertical">
- <TextBlock Text="***塊文本"/>
- <TextBlock Text="第二塊文本"/>
- <TextBlock Text="第三塊文本"/>
- <TextBlock Text="第四塊文本"/>
- </StackPanel>
怎么做呢?因?yàn)樽痔?hào)為Double類型,所以首先要引入命名空間。怎么做呢?因?yàn)樽痔?hào)為Double類型,所以首先要引入命名空間。
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
接著,在頁資源集合中定義一個(gè)字號(hào)資源,注意要設(shè)置key,每個(gè)資源都有***的鍵,應(yīng)用程序是通過這個(gè)鍵來尋找對(duì)應(yīng)的資源的。接著,在頁資源集合中定義一個(gè)字號(hào)資源,注意要設(shè)置key,每個(gè)資源都有***的鍵,應(yīng)用程序是通過這個(gè)鍵來尋找對(duì)應(yīng)的資源的。
- <StackPanel Orientation="Vertical">
- <TextBlock Text="***塊文本" FontSize="{StaticResource fontSize}" />
- <TextBlock Text="第二塊文本" FontSize="{StaticResource fontSize}" />
- <TextBlock Text="第三塊文本" FontSize="{StaticResource fontSize}" />
- <TextBlock Text="第四塊文本" FontSize="{StaticResource fontSize}" />
- </StackPanel>
資源的引用方式很簡(jiǎn)單,放到一對(duì)大括號(hào)中(擴(kuò)展標(biāo)記),StaticResource是指明是靜態(tài)資源,注意,在Silverlight中只能用靜態(tài)資源,如果是WPF,還有動(dòng)態(tài)資源,空格后面就是資源的key,不要問我為什么。
再看一例,有三個(gè)按鈕,我希望它們都擁有漸變背景色,水平左對(duì)齊,垂直頂端對(duì)齊,寬185,高50.
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Button Content="按鈕一" Height="72" Margin="10,10,0,0" Name="button1" />
- <Button Content="按鈕二" Height="72" Margin="10,92,0,0" Name="button2" />
- <Button Content="按鈕三" Height="72" Margin="10,174,0,0" Name="button3" />
- </Grid>
現(xiàn)在我只要在資源集合里聲明一個(gè)樣式,并把它應(yīng)用到每個(gè)按鈕上。
- <phone:PhoneApplicationPage
- x:Class="ResSampleApp.Page2"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
- shell:SystemTray.IsVisible="True">
- <phone:PhoneApplicationPage.Resources>
- <Style x:Key="buttonStyle" TargetType="Button">
- <Setter Property="Background">
- <Setter.Value>
- <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
- <GradientStop Color="Yellow" Offset="0"/>
- <GradientStop Color="Red" Offset="1"/>
- </LinearGradientBrush>
- </Setter.Value>
- </Setter>
- <Setter Property="HorizontalAlignment" Value="Left"/>
- <Setter Property="VerticalAlignment" Value="Top"/>
- <Setter Property="Width" Value="185"/>
- <Setter Property="Height" Value="50"/>
- <Setter Property="BorderThickness" Value="0"/>
- </Style>
- </phone:PhoneApplicationPage.Resources>
- <Grid>
- <Button Content="按鈕一" Height="72" Margin="10,10,0,0" Name="button1" Style="{StaticResource buttonStyle}" />
- <Button Content="按鈕二" Height="72" Margin="10,92,0,0" Name="button2" Style="{StaticResource buttonStyle}" />
- <Button Content="按鈕三" Height="72" Margin="10,174,0,0" Name="button3" Style="{StaticResource buttonStyle}" />
- </Grid>
- </phone:PhoneApplicationPage>