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.     } 

效果图:



本文转自shenzhoulong  51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/862631,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值