ApplicationBar control is a menu on Windows Phone 7, which is a traditional windowsProgram.
The classes related to ApplicationBar (applicationbariconbutton and applicationbarmenuitem) are defined in the Microsoft. Phone. Shell namespace. The class layers of conventional Silverlight programming such as uielement and frameworkelement are completely separated. Strictly speaking, ApplicationBar is not part of your page visualization.
An ApplicationBar can contain up to four buttons. You can use menu items to add additional options. These menu items are not displayed by default. It is displayed only when you click the ellipsis (or area below the ellipsis) on the right of the menu bar.
This project contains a mediaelement movieplayer to play a video, and ApplicationBar contains the first, play, pause, and last four options.
<! -- Layoutroot contains the root grid where all other page content is placed -->
< Grid X: Name = "Layoutroot" Background = "Transparent" >
< Grid. rowdefinitions >
< Rowdefinition Height = "Auto" />
< Rowdefinition Height = "*" />
</ Grid. rowdefinitions >
<! -- Titlepanel contains the name of the application and page title -->
< Stackpanel X: Name = "Titlepanel" Grid. Row = "0" Margin =" >
< Textblock X: Name = "Applicationtitle" Text = "Movie player" Style =" {Staticresource phonetextnormalstyle} " />
</ Stackpanel >
< Grid X: Name = "Contentpanel" Grid. Row = "1" Margin = "12, 0, 12, 0" >
< Mediaelement Name = "Mediaelement"
Source = "Http: // localhost/123.wmv"
Autoplay = "False"
Mediaopened = "Onmediaelementmediaopened"
Mediafailed = "Onmediaelementmediafailed"
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 >
< Shell: applicationbariconbutton
X: Name = "Appbarrewindbutton"
Iconuri = "Images/appbar.transport.remo-rest.png"
Text = "Rewind"
Isenabled = "False"
Click = "Onappbarrewindclick" />
< Shell: applicationbariconbutton
X: Name = "Appbarplaybutton"
Iconuri = "Images/appbar.transport.play.rest.png"
Text = "Play"
Isenabled = "False"
Click = "Onappbarplayclick" />
< Shell: applicationbariconbutton
X: Name = "Appbarpausebutton"
Iconuri = "Images/appbar.transport.pause.rest.png"
Text = "Pause"
Isenabled = "False"
Click = "Onappbarpauseclick" />
< Shell: applicationbariconbutton
X: Name = "Appbarendbutton"
Iconuri = "Images/appbar.transport.ff.rest.png"
Text = "To end"
Isenabled = "False"
Click = "Onappbarendclick" />
</ Shell: ApplicationBar >
</ Phone: phoneapplicationpage. ApplicationBar >
View code
Using System;
Using System. windows;
Using System. Windows. Media;
Using Microsoft. Phone. controls;
Using Microsoft. Phone. shell;
Namespace Movieplayer
{
Public Partial Class Mainpage: phoneapplicationpage
{
// Constructor
Public Mainpage ()
{
Initializecomponent ();
// Re-assign names already in the XAML File
Appbarrewindbutton = This . ApplicationBar. Buttons [ 0 ] As Applicationbariconbutton;
Appbarplaybutton = This . ApplicationBar. Buttons [ 1 ] As Applicationbariconbutton;
Appbarpausebutton = This . ApplicationBar. Buttons [ 2 ] As Applicationbariconbutton;
Appbarendbutton = This . ApplicationBar. Buttons [ 3 ] As Applicationbariconbutton;
}
// ApplicationBar buttons
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;
}
// Mediaelement events
Void Onmediaelementmediafailed ( Object Sender, exceptionroutedeventargs ARGs)
{
Errortext. Text = Args. errorexception. message;
}
Void Onmediaelementmediaopened ( Object Sender, routedeventargs ARGs)
{
Appbarrewindbutton. isenabled = True ;
Appbarendbutton. isenabled = True ;
}
Void Onmediaelementcurrentstatechanged ( Object Sender, routedeventargs ARGs)
{
Statustext. Text = Mediaelement. currentstate. tostring ();
If (Mediaelement. currentstate = Mediaelementstate. Stopped |
Mediaelement. currentstate = Mediaelementstate. paused)
{
Appbarplaybutton. isenabled = True ;
Appbarpausebutton. isenabled = False ;
}
Else If (Mediaelement. currentstate = Mediaelementstate. Playing)
{
Appbarplaybutton. isenabled = False ;
Appbarpausebutton. isenabled = True ;
}
}
}
}