Windows Phone開發(15):資源

活字印刷術是我國“四大發明”之一,畢昇在發明活字印刷術之後,他很快發現一個問題,隨着要印刷資料的不斷增加,要用到的漢字數目越來越多,於是,他必須尋找一種有效的辦法去管理那些刻有漢字的立方體(暫且就叫立方體,其實的確是個立方體),所以,他就和助手們一起努力,爲這些立方體進行記錄,有標識地放好,在印刷過程中用到哪些字,就直接取出來,不用了就放回去,既環保又方便。
這就是資源,水、空氣、陽光也是資源,煤、鐵礦物也是資源,只不過有些可再生,有些不可再生罷了。
何爲資源?資源就是客觀存在的,當我們需要時可以拿來利用的一切可支配或可重新組合的東西,如人力資源、人脈資源等。
如果做過網頁,應該瞭解CSS是用來幹啥的,其實,我們今天要討論的資源,和CSS樣式表的概念基本一樣,就是把一些經常用到的東西保存起來,可以供應用程序中不同地方重複調用,這樣我們就不用爲每個控件設置樣式,我們可以樣式保存到資源列表,用到就取出來,不用重複定義。

下面看看這段XAML,上面有4個TextBlock,我現在希望每個TextBlock的字體字號爲37.5,當然,簡單的值可以方便設置,如果值很複雜,如上一篇文章說的模板,那你就很痛苦了,要爲每個控件做一個模板。

  1. <StackPanel Orientation="Vertical">  
  2.     <TextBlock Text="第一塊文本"/>  
  3.     <TextBlock Text="第二塊文本"/>  
  4.     <TextBlock Text="第三塊文本"/>  
  5.     <TextBlock Text="第四塊文本"/>  
  6. </StackPanel>  


怎麼做呢?因爲字號爲Double類型,所以首先要引入命名空間。怎麼做呢?因爲字號爲Double類型,所以首先要引入命名空間。

  1. xmlns:sys="clr-namespace:System;assembly=mscorlib"  


接着,在頁資源集合中定義一個字號資源,注意要設置key,每個資源都有唯一的鍵,應用程序是通過這個鍵來尋找對應的資源的。接着,在頁資源集合中定義一個字號資源,注意要設置key,每個資源都有唯一的鍵,應用程序是通過這個鍵來尋找對應的資源的。

  1. <StackPanel Orientation="Vertical">  
  2.     <TextBlock Text="第一塊文本" FontSize="{StaticResource fontSize}" />  
  3.     <TextBlock Text="第二塊文本" FontSize="{StaticResource fontSize}" />  
  4.     <TextBlock Text="第三塊文本" FontSize="{StaticResource fontSize}" />  
  5.     <TextBlock Text="第四塊文本" FontSize="{StaticResource fontSize}" />  
  6. </StackPanel>  


 

資源的引用方式很簡單,放到一對大括號中(擴展標記),StaticResource是指明是靜態資源,注意,在Silverlight中只能用靜態資源,如果是WPF,還有動態資源,空格後面就是資源的key,不要問我爲什麼。

再看一例,有三個按鈕,我希望它們都擁有漸變背景色,水平左對齊,垂直頂端對齊,寬185,高50.

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.     <Button Content="按鈕一" Height="72"  Margin="10,10,0,0" Name="button1"  />  
  3.     <Button Content="按鈕二" Height="72"  Margin="10,92,0,0" Name="button2"  />  
  4.     <Button Content="按鈕三" Height="72"  Margin="10,174,0,0" Name="button3"  />  
  5. </Grid>  


現在我只要在資源集合裏聲明一個樣式,並把它應用到每個按鈕上。

  1. <phone:PhoneApplicationPage   
  2.     x:Class="ResSampleApp.Page2"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  10.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  11.     Foreground="{StaticResource PhoneForegroundBrush}"  
  12.     SupportedOrientations="Portrait" Orientation="Portrait"  
  13.     mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"  
  14.     shell:SystemTray.IsVisible="True">  
  15.     <phone:PhoneApplicationPage.Resources>  
  16.         <Style x:Key="buttonStyle" TargetType="Button">  
  17.             <Setter Property="Background">  
  18.                 <Setter.Value>  
  19.                     <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">  
  20.                         <GradientStop Color="Yellow" Offset="0"/>  
  21.                         <GradientStop Color="Red" Offset="1"/>  
  22.                     </LinearGradientBrush>  
  23.                 </Setter.Value>  
  24.             </Setter>  
  25.             <Setter Property="HorizontalAlignment" Value="Left"/>  
  26.             <Setter Property="VerticalAlignment" Value="Top"/>  
  27.             <Setter Property="Width" Value="185"/>  
  28.             <Setter Property="Height" Value="50"/>  
  29.             <Setter Property="BorderThickness" Value="0"/>  
  30.         </Style>  
  31.     </phone:PhoneApplicationPage.Resources>  
  32.   
  33.     <Grid>  
  34.         <Button Content="按鈕一" Height="72"  Margin="10,10,0,0" Name="button1" Style="{StaticResource buttonStyle}" />  
  35.         <Button Content="按鈕二" Height="72"  Margin="10,92,0,0" Name="button2" Style="{StaticResource buttonStyle}" />  
  36.         <Button Content="按鈕三" Height="72"  Margin="10,174,0,0" Name="button3" Style="{StaticResource buttonStyle}" />  
  37.     </Grid>  
  38.   
  39.   
  40. </phone:PhoneApplicationPage>  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章