ApplicationBarIconButton Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

An Application Bar button with an icon.

Inheritance Hierarchy

System..::.Object
  Microsoft.Phone.Shell..::.ApplicationBarIconButton

Namespace:  Microsoft.Phone.Shell
Assembly:  Microsoft.Phone (in Microsoft.Phone.dll)
XMLNS for XAML: Not mapped to an xmlns.

Syntax

Public Class ApplicationBarIconButton _
    Implements IApplicationBarIconButton, IApplicationBarMenuItem
public class ApplicationBarIconButton : IApplicationBarIconButton, 
    IApplicationBarMenuItem
<ApplicationBarIconButton .../>

The ApplicationBarIconButton type exposes the following members.

Constructors

  Name Description
ApplicationBarIconButton()()() Creates a new instance of the ApplicationBarIconButton class.
ApplicationBarIconButton(Uri) Creates a new instance of the ApplicationBarIconButton class with the specified icon.

Top

Properties

  Name Description
IconUri Gets or sets the URI of the icon to use for the button.
IsEnabled Gets or sets the enabled status of the button.
Text Gets or sets the label for the icon button.

Top

Methods

  Name Description
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Events

  Name Description
Click Occurs when the user taps a button in the Application Bar.

Top

Examples

The following example creates an Application Bar in XAML that has two icon buttons and two menu items. For the full example, see How to create an app bar using XAML for Windows Phone.

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar Mode="Default" Opacity="1.0" IsMenuEnabled="True" IsVisible="True">

        <shell:ApplicationBarIconButton Click="Save_Click" IconUri="/Images/save.png" Text="save" />
        <shell:ApplicationBarIconButton Click="Settings_Click" IconUri="/Images/settings.png" Text="settings" />

        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Click="MenuItem1_Click" Text="menu item 1" />
            <shell:ApplicationBarMenuItem Click="MenuItem2_Click" Text="menu item 2" />
        </shell:ApplicationBar.MenuItems>

    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

The following example creates an Application Bar in code that has one icon button and one menu item. For the full example, see How to create an app bar using code for Windows Phone.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace HowToCS
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            ApplicationBar = new ApplicationBar();

            ApplicationBar.Mode = ApplicationBarMode.Default;
            ApplicationBar.Opacity = 1.0; 
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = true;

            ApplicationBarIconButton button1 = new ApplicationBarIconButton();
            button1.IconUri = new Uri("/Images/YourImage.png", UriKind.Relative);
            button1.Text = "button 1";
            ApplicationBar.Buttons.Add(button1);
            button1.Click += new EventHandler(button1_Click);

            ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem();
            menuItem1.Text = "menu item 1";
            ApplicationBar.MenuItems.Add(menuItem1);
            menuItem1.Click += new EventHandler(menuItem1_Click);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button 1 works!");
            //Do work for your application here.
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Menu item 1 works!");
            //Do work for your application here.
        }
    }
}
Imports Microsoft.Phone.Shell

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    ' Constructor
    Public Sub New()
        InitializeComponent()
        
        ApplicationBar = new ApplicationBar()

        ApplicationBar.Mode = ApplicationBarMode.Default
        ApplicationBar.Opacity = 1.0
        ApplicationBar.IsVisible = true
        ApplicationBar.IsMenuEnabled = true

        Dim button1 as ApplicationBarIconButton = new ApplicationBarIconButton()
        button1.IconUri = new Uri("/Images/YourImage.png", UriKind.Relative)
        button1.Text = "button 1"
        ApplicationBar.Buttons.Add(button1)
        AddHandler button1.Click, AddressOf button1_Click

        Dim menuItem1 as ApplicationBarMenuItem = new ApplicationBarMenuItem()
        menuItem1.Text = "menu item 1"
        ApplicationBar.MenuItems.Add(menuItem1)
        AddHandler menuItem1.Click, AddressOf menuItem1_Click
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("Button 1 works!")
        'Do work for your application here.
    End Sub

    Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("Menu item 1 works!")
        'Do work for your application here.
    End Sub
End Class

The following example dynamically changes the caption text and icon of a button on an Application Bar. For the full example, see How to change app bar icon buttons and menu items dynamically for Windows Phone.

private void button1_Click(object sender, EventArgs e)
{
    ApplicationBarIconButton btn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];

    if (btn.Text == "play")
    {
        btn.Text="pause";
        btn.IconUri = new Uri("/Images/pause.png", UriKind.Relative);
    }
    else if(btn.Text == "pause")
    {
        btn.Text="play";
        btn.IconUri = new Uri("/Images/play.png", UriKind.Relative);
    }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim btn As ApplicationBarIconButton = ApplicationBar.Buttons(0)

    If btn.Text = "play" Then

        btn.Text="pause"
        btn.IconUri = New Uri("/Images/pause.png", UriKind.Relative)

    ElseIf btn.Text="pause" Then

        btn.Text="play"
        btn.IconUri = New Uri("/Images/play.png", UriKind.Relative)
    End If
End Sub

The following example creates an Application Bar that can be reused on multiple pages in your application. For the full example, see How to reuse an app bar on multiple pages for Windows Phone.

<Application.Resources>

    <shell:ApplicationBar x:Key="GlobalAppBar" IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="Button1_Click" />
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2" Click="Button2_Click" />
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1" Click="MenuItem1_Click" />
            <shell:ApplicationBarMenuItem Text="MenuItem 2" Click="MenuItem2_Click" />
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>

</Application.Resources>

The following example creates two Application Bars that can be used in a single pivot control in your application. For the full example, see How to use different app bars in a single Pivot control for Windows Phone.

<Application.Resources>

    <shell:ApplicationBar x:Key="AppBar1" IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="Button1_Click" />
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1" Click="MenuItem1_Click" />
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>

    <shell:ApplicationBar x:Key="AppBar2" IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="Button1_Click" />
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2" Click="Button2_Click" />
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1" Click="MenuItem1_Click" />
            <shell:ApplicationBarMenuItem Text="MenuItem 2" Click="MenuItem2_Click" />
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
        
</Application.Resources>

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.Phone.Shell Namespace

Other Resources

App bar for Windows Phone

App bar icon buttons for Windows Phone

How to change app bar icon buttons and menu items dynamically for Windows Phone