[Windows Phone]Application Bar

1. Xaml Code inside root Grid element

 <phone:PhoneApplicationPage.ApplicationBar>
     <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" 
                           Opacity="0.5" Mode="Default">
         <shell:ApplicationBarIconButton x:Name="appBar_Save"
                                         IconUri="/Images/appbar_button1.png" 
                                         Text="Save"
                                         Click="appBar_Save_Click" />
         <shell:ApplicationBarIconButton x:Name="appBar_Cancel"
                                         IconUri="/Images/appbar_button2.png" 
                                         Text="Button 2"
                                         Click="appBar_Cancel_Click" />
         <shell:ApplicationBar.MenuItems>
             <shell:ApplicationBarMenuItem x:Name="appMenu_Item1" 
                                           Text="MenuItem 1"
                                           Click="appMenu_Item1_Click"/>
             <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
             <shell:ApplicationBarMenuItem Text="MenuItem 3"/>
         </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

2. Backend c# code:

public partial class MainPage : PhoneApplicationPage {
   // Constructor
   public MainPage() {
       InitializeComponent();
   }
   private void btn_goPivotPage_Click(object sender, RoutedEventArgs e) 
{    NavigationService.Navigate(new Uri("/PivotPage.xaml"UriKind.Relative));    }    private void appBar_Save_Click(object sender, EventArgs e)    {        Dispatcher.BeginInvoke(() => { MessageBox.Show("Save clicked"); });    }    private void appBar_Cancel_Click(object sender, EventArgs e)    {        Dispatcher.BeginInvoke(() => { MessageBox.Show("Cancel clicked"); });    }    private void appMenu_Item1_Click(object sender, EventArgs e)    {        Dispatcher.BeginInvoke(() => { MessageBox.Show("Item1 clicked"); });    } }

3.To get App bar button object

var saveBtn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
var menuItem1 = (ApplicationBarIconButton)ApplicationBar.MenuItems[0];

 

blog comments powered by Disqus