??Microsoft.Phone.Shell命名空間中定義了ApplicationBar及其相關類(ApplicationBarIconButton和ApplicationBarMenuItem),這些類派生自Object,并完全獨立于常規Silverlight編程中的DependencyObject,UIElement和FrameworkElement類層次結構。ApplicationBar最多能包含4個按鈕,包含的圖片通常是PNG文件,位圖本身的寬高都是48像素,通常是透明的,實際圖片應該是白色,在位圖中間顯示,是一個寬高均為26像素的正方形。
??????? <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.rew.rest.png" Text="上一首"/>
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.play.rest.png" Text="播放"/>
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.pause.rest.png" Text="暫停"/>
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.ff.rest.png" Text="下一首"/>
??? </phone:PhoneApplicationPage.ApplicationBar>
當你點擊省略號的時候,出現如下效果:
?
在這里,我們可以通過將ApplicationBarIconButton的IsEnabled屬性設為false,從而禁用該按鈕。
?
上面的2張圖的第一個ApplicationBarIconButton的圖片放錯了。?
?
一般情況下,我們要訪問這些按鈕可以如下做:
(this.ApplicationBar.Button[2] as ApplicationBarIconButton).IsEnabled=true or false
?
我們改造前面的demo來實現一個播放網絡視頻的案例來進一步的學習ApplicationBar的使用。
?
?
XAML文件:
<phone:PhoneApplicationPage
??? x:Class="PhoneApp3.MainPage"
??? 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"
??? mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
??? FontFamily="{StaticResource PhoneFontFamilyNormal}"
??? FontSize="{StaticResource PhoneFontSizeNormal}"
??? Foreground="{StaticResource PhoneForegroundBrush}"
??? SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
??? shell:SystemTray.IsVisible="True">
??? <!--LayoutRoot 是包含所有頁面內容的根網格-->
??? <Grid x:Name="LayoutRoot" Background="Transparent">
??????? <Grid.RowDefinitions>
??????????? <RowDefinition Height="Auto"/>
??????????? <RowDefinition Height="*"/>
??????? </Grid.RowDefinitions>
??????? <!--TitlePanel 包含應用程序的名稱和頁標題-->
??????? <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
??????????? <TextBlock x:Name="ApplicationTitle" Text="電影播放" Style="{StaticResource PhoneTextNormalStyle}"/>
??????????? <TextBlock x:Name="PageTitle" Text="恐怖片" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
??????? </StackPanel>
??????? <!--ContentPanel - 在此處放置其他內容-->
??????? <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
?????????
??????????? <MediaElement Name="mediaElement" Source="http://www.charlespetzold.com/Media/Walrus.wmv" AutoPlay="False" MediaOpened="onMediaElementOpened" MediaFailed="onMediaElementFailed" CurrentStateChanged="onMediaElementCurrentStateChanged"/>
???????????
??????????? <TextBlock Name="statusText" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
??????????? <TextBlock Name="errorText" HorizontalAlignment="Right" VerticalAlignment="Bottom" TextWrapping="Wrap"/>
??????? </Grid>
??? </Grid>
?
??? <phone:PhoneApplicationPage.ApplicationBar>
??????? <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.rew.rest.png" Text="重置"??? Click="onAppbarRewindClick" x:Name="appbarRewind" IsEnabled="False"/>
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.play.rest.png" Text="播放" Click="onAppbarPlayClick" x:Name="appBarPlay" />
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.pause.rest.png" Text="暫停" Click="onAppbarPauseClick" x:Name="appBarPause" IsEnabled="False"/>
??????????? <shell:ApplicationBarIconButton IconUri="/Images/appbar.transport.ff.rest.png" Text="結束" Click="onAppbarEndClick" x:Name="appbarEnd" IsEnabled="False"/>
??????? </shell:ApplicationBar>
??? </phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>
?
代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace PhoneApp3
{
??? public partial class MainPage : PhoneApplicationPage
??? {
??????? // 構造函數
??????? public MainPage()
??????? {
??????????? InitializeComponent();
??????????? appbarRewind = this.ApplicationBar.Buttons[0] as ApplicationBarIconButton;
??????????? appBarPlay = this.ApplicationBar.Buttons[1] as ApplicationBarIconButton;
??????????? appBarPause = this.ApplicationBar.Buttons[2] as ApplicationBarIconButton;
??????????? appbarEnd = this.ApplicationBar.Buttons[3] as ApplicationBarIconButton;
??????? }
??????? void onAppbarRewindClick(object sender, EventArgs args)
??????? {
??????????? mediaElement.Position = TimeSpan.Zero;
??????? }
??????? void onAppbarPlayClick(object sender,EventArgs args) {
??????????? mediaElement.Play();
??????? }
??????? void onAppbarPauseClick(object sender, EventArgs args)
??????? {
??????????? mediaElement.Pause();
??????? }
??????? void onAppbarEndClick(object sender,EventArgs args) {
??????????? mediaElement.Position = mediaElement.NaturalDuration.TimeSpan;
??????? }
??????? void onMediaElementFailed(object sender,ExceptionRoutedEventArgs args)
??????? {
??????????? errorText.Text = args.ErrorException.Message;
??????? }
??????? void onMediaElementOpened(object sender, RoutedEventArgs args)
??????? {
??????????? appbarEnd.IsEnabled = true;
??????????? appbarRewind.IsEnabled = true;
??????? }
??????? void onMediaElementCurrentStateChanged(object sender, RoutedEventArgs ars)
??????? {
??????????? statusText.Text = mediaElement.CurrentState.ToString();
??????????? if (mediaElement.CurrentState == MediaElementState.Stopped || mediaElement.CurrentState == MediaElementState.Paused)
??????????? {
??????????????? appBarPlay.IsEnabled = true;
??????????????? appBarPause.IsEnabled = false;
??????????? }
??????????? else if(mediaElement.CurrentState==MediaElementState.Playing)
??????????? {
??????????????? appBarPause.IsEnabled = true;
??????????????? appBarPlay.IsEnabled = false;
??????????? }
??????? }
????
??? }
}
?
Enjoy yourself.?