Programmatically click App Bar buttons


  • Hi,  Is there any way we can simulate programmatically clicking an app bar button?.

    We can programatically have a button click event, but can we achieve the same for app bar buttons?.

    Thanks In Advance.

    Saturday, July 12, 2014 5:34 PM

All replies

  • Hello,

    you can call the click event by code for the app bar button.

    E.g. the definition for the button in XAML:

    <phone:PhoneApplicationPage.ApplicationBar>     <shell:ApplicationBar Mode="Minimized">
    <shell:ApplicationBarIconButton x:Name="apbButton" IconUri="YourPicture.png" Text="Button" Click="apbButton_Click"/> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>

    If this is your event handler for the click event for the button:

    Private Sub apbButton_Click(sender As Object, e As EventArgs)
        'do something
    End Sub

    you can call it also from inside your code by

    apbButton_Click(Me, New System.EventArgs)

    Good evening


    Saturday, July 12, 2014 7:53 PM
  • Yes you could do the same Click event for the App Bar too as you did for the button. But make sure that you have that within the ApplicationBarIconButton or for the MenuItem within the App Bar.

    Sunday, July 13, 2014 5:49 AM
  • Hey thanks for the reply.

    I used Button Automation Peer variable for automatic button click, but the same is not working for App Bar button. And yes I have the app bar buttons within ApplicationBarIconButton. 

    Sunday, July 13, 2014 5:53 AM
  • Hey thanks for the reply. But what should be the sender value while calling the click function dynamically.
    Sunday, July 13, 2014 6:22 AM
  • The sender value is normally the button object, but do you actually use any of the arguments in your click handler code? If not, you can just call it with null arguments.

    However, it would be better programming practice if you create a separate method that performs the action you want to do. Call this method in the click handler, and in your other circumstances.

            private void Button_Click(object sender, RoutedEventArgs e)
            void PerformClickAction()
                // do something
            void OtherCode()

    Sunday, July 13, 2014 8:04 AM