windows phone (26) ApplicationBar應用程序欄

 在應用程序中,如果需要幾個按鈕或者菜單來執行一些普通的命令,就應該考慮使用ApplicationBar,因爲silverlight並沒有定義任何常用的菜單或者工具,我們通常稱ApplicationBar爲應用程序欄,該類定義在命名空間Microsoft.Phone.Shell中, 在改命名空間中還定義了ApplicationBarIconButton和ApplicationBarMenuItem,這些類都派生自Object 而非DeendecyObject,UIElement和FramworkElement類,嚴格的說ApplicationBar並不是可視化樹的一部 分(未映射到 xmlns),ApplicationBar對象在xaml文件中作爲PhoneApplicationPage的ApplicationBar屬性存 在,當手機水平放置或者垂直放置時都是一樣的效果,且無法自定義ApplicationBar;ApplicationBar最多可以包含四個按鈕,因爲 一般使用圖片進行設置按鈕,這些按鈕通常稱之爲圖標,且圖標一般爲png格式,圖標的寬和高都應爲48像素,並通常是透明的;【作者:神舟龍

示例

下面的示例就是實現一個簡易的播放器,並且有播放,暫停,重新開始和轉至結尾,四個功能圖標,首先從新浪下載圖標,參考書給的微軟的下載地址已經刪除 ,並在項目裏建立一個Image文件,用於保存四個圖片

 

 並 把每個圖片的屬性中的【生成操作】設置爲內容,如果設置生成操作爲Resource,ApplicationBar就無法智能的找到這些圖像;因爲 ApplicationBar不是標準的silverlight的一部分,因此XML命名空間聲明需要將XML的“Shell”命名空間與.NET命名空 間Microsoft.Phone.Shell關聯起來,標準的MainPage.xaml已經爲我們做好了這些

  1. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 

下面就是設置這四個按鈕,我們直接把IDE已經註釋掉的部分重新啓用,稍微改一下就ok

  1. <!--演示 ApplicationBar 用法的示例代碼--> 
  2.     <phone:PhoneApplicationPage.ApplicationBar> 
  3.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
  4.             <shell:ApplicationBar.Buttons> 
  5.             <shell:ApplicationBarIconButton x:Name="appbarRewindButton" IconUri="/Images/appbar.transport.rew.rest.png" Text="重置" IsEnabled="False" Click="appbarRewindButton_Click"/> 
  6.             <shell:ApplicationBarIconButton x:Name="appbarPlayButton" IconUri="/Images/appbar.transport.play.rest.png" Text="開始"  IsEnabled="False" Click="appbarPlayButton_Click"/> 
  7.             <shell:ApplicationBarIconButton x:Name="appbarPauseButton" IconUri="/Images/appbar.transport.pause.rest.png" Text="暫定"  IsEnabled="False" Click="appbarPauseButton_Click"/> 
  8.             <shell:ApplicationBarIconButton x:Name="appbarEndButton" IconUri="/Images/appbar.transport.ff.rest.png" Text="結束"  IsEnabled="False" Click="appbarEndButton_Click"/> 
  9.             </shell:ApplicationBar.Buttons> 
  10.         </shell:ApplicationBar> 
  11.     </phone:PhoneApplicationPage.ApplicationBar> 

從上面代碼中可以看到ApplicationBar有一個Buttons屬性,該屬性是該類的內容屬性,所以該屬性不是必須出現的,Buttons集合最 多可以包含四個ApplicationBarIconButton 對象,每個ApplicationBarIconButton 對象可以設置ICONURI,X:Name和Text,其中ICONURI表示路徑,Text表示說明文字;當你按下一個圖標時,該圖標會產生一定的偏 移,作爲操作反饋,如果按下省略號,則會把Text設置的文字進行顯示

如果你不希望ApplicationBar在開始時進行顯示,可以設置

  1. <shell:ApplicationBar IsVisible="False"> 

ApplicationBar 也定義了前景色和背景色,如果改變了手機的主題顏色,那麼默認的ApplicationBar 顏色也會有改變

ApplicationBar 還可以設置Opacity屬性,默認情況下是1,此時ApplicationBar佔用頁面內容區域之外的區域空間,如果設置爲其他值比如0.5,此時 ApplicationBar則與頁面的其他內容共享空間,但是圖標總是在最前端顯示,文檔建議設置值爲1,0.5,0
 
  1. <shell:ApplicationBar IsVisible="True" Opacity="0.5"> 

效果:

 

 

 從上面代碼中可以看到每個圖標都是被禁用的IsEnabled="False",那麼怎麼從隱藏文件代碼設置禁用那,前面說過ApplicationBarIconButton 是在buttons集合中的所以我們可以用索引的形式獲得某個圖標,並設置屬性,比如最後一個圖標禁用可以這樣寫

  1. ((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).IsEnabled = false

 同理可以在隱藏代碼中設置其他的屬性值

 

下面是示例的主要代碼

xaml文件代碼:

 

  1. View Code  <!--LayoutRoot 是包含所有頁面內容的根網格--> 
  2.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  3.         <Grid.RowDefinitions> 
  4.             <RowDefinition Height="Auto"/> 
  5.             <RowDefinition Height="*"/> 
  6.         </Grid.RowDefinitions> 
  7.  
  8.         <!--TitlePanel 包含應用程序的名稱和頁標題--> 
  9.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  10.             <TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/> 
  11.             <TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  12.         </StackPanel> 
  13.  
  14.         <!--ContentPanel - 在此處放置其他內容--> 
  15.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="DarkCyan"> 
  16.             <MediaElement Name="mediaElement" Source="http://www.charlespetzold.com/Media/Walrus.wmv" 
  17.              AutoPlay="False" MediaFailed="mediaElement_MediaFailed" MediaOpened="mediaElement_MediaOpened" 
  18.              CurrentStateChanged="mediaElement_CurrentStateChanged"  
  19.              ></MediaElement> 
  20.             <!--顯示狀態--> 
  21.             <TextBlock x:Name="statusText" HorizontalAlignment="Left" VerticalAlignment="Bottom"></TextBlock> 
  22.             <!--顯示錯誤信息--> 
  23.             <TextBlock x:Name="errorText" HorizontalAlignment="Right" VerticalAlignment="Bottom" TextWrapping="Wrap"></TextBlock> 
  24.         </Grid> 
  25.     </Grid> 
  26.   
  27.     <!--演示 ApplicationBar 用法的示例代碼--> 
  28.     <phone:PhoneApplicationPage.ApplicationBar> 
  29.         <shell:ApplicationBar IsVisible="True"> 
  30.             <shell:ApplicationBar.Buttons> 
  31.             <shell:ApplicationBarIconButton x:Name="appbarRewindButton" IconUri="/Images/appbar.transport.rew.rest.png" Text="重置" IsEnabled="False" Click="appbarRewindButton_Click"/> 
  32.             <shell:ApplicationBarIconButton x:Name="appbarPlayButton" IconUri="/Images/appbar.transport.play.rest.png" Text="開始"  IsEnabled="False" Click="appbarPlayButton_Click"/> 
  33.             <shell:ApplicationBarIconButton x:Name="appbarPauseButton" IconUri="/Images/appbar.transport.pause.rest.png" Text="暫定"  IsEnabled="False" Click="appbarPauseButton_Click"/> 
  34.             <shell:ApplicationBarIconButton x:Name="appbarEndButton" IconUri="/Images/appbar.transport.ff.rest.png" Text="結束"  IsEnabled="False" Click="appbarEndButton_Click"/> 
  35.             </shell:ApplicationBar.Buttons>        
  36.         </shell:ApplicationBar> 
  37.         
  38.     </phone:PhoneApplicationPage.ApplicationBar> 

 代碼影藏文件,需要先引入命名空間

  1. using Microsoft.Phone.Shell; 

 隱藏文件全部代碼如下

  1. View Code using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. //ApplicationBarIconButton用到 
  14. using Microsoft.Phone.Shell; 
  15.  
  16. namespace MoviePlayer 
  17.     public partial class MainPage : PhoneApplicationPage 
  18.     { 
  19.         // 構造函數 
  20.         public MainPage() 
  21.         { 
  22.             InitializeComponent(); 
  23.             appbarEndButton=(ApplicationBarIconButton)this.ApplicationBar.Buttons[3]; 
  24.             appbarPauseButton = (ApplicationBarIconButton)this.ApplicationBar.Buttons[2]; 
  25.             appbarPlayButton = (ApplicationBarIconButton)this.ApplicationBar.Buttons[1]; 
  26.             appbarRewindButton = (ApplicationBarIconButton)this.ApplicationBar.Buttons[0]; 
  27.             
  28.         } 
  29.         /// <summary> 
  30.         /// 失敗時發生 
  31.         /// </summary> 
  32.         /// <param name="sender"></param> 
  33.         /// <param name="e"></param> 
  34.         private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e) 
  35.         { 
  36.             eerrorText.Text = e.ErrorException.Message; 
  37.         } 
  38.         /// <summary> 
  39.         /// 打開時發生 
  40.         /// </summary> 
  41.         /// <param name="sender"></param> 
  42.         /// <param name="e"></param> 
  43.         private void mediaElement_MediaOpened(object sender, RoutedEventArgs e) 
  44.         { 
  45.             appbarRewindButton.IsEnabled = true
  46.             appbarEndButton.IsEnabled = true
  47.  
  48.         } 
  49.         /// <summary> 
  50.         /// 狀態更改是發生 
  51.         /// </summary> 
  52.         /// <param name="sender"></param> 
  53.         /// <param name="e"></param> 
  54.         private void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e) 
  55.         { 
  56.             statusText.Text = mediaElement.CurrentState.ToString(); 
  57.             if (mediaElement.CurrentState==MediaElementState.Stopped||mediaElement.CurrentState==MediaElementState.Paused) 
  58.             { 
  59.                 appbarPlayButton.IsEnabled = true
  60.                 appbarPauseButton.IsEnabled = false
  61.             } 
  62.             else if (mediaElement.CurrentState==MediaElementState.Playing) 
  63.             { 
  64.                 appbarPlayButton.IsEnabled = false
  65.                 appbarPauseButton.IsEnabled = true
  66.             } 
  67.         } 
  68.         //重置 
  69.         private void appbarRewindButton_Click(object sender, EventArgs e) 
  70.         { 
  71.             mediaElement.Position = TimeSpan.Zero; 
  72.         } 
  73.         //播放 
  74.         private void appbarPlayButton_Click(object sender, EventArgs e) 
  75.         { 
  76.             mediaElement.Play(); 
  77.         } 
  78.         //暫定 
  79.         private void appbarPauseButton_Click(object sender, EventArgs e) 
  80.         { 
  81.             mediaElement.Pause(); 
  82.         } 
  83.         //結束 
  84.         private void appbarEndButton_Click(object sender, EventArgs e) 
  85.         { 
  86.             mediaElementmediaElement.Position = mediaElement.NaturalDuration.TimeSpan; 
  87.         } 
  88.     } 

效果圖:

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