Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically click an ApplicationBarIconButton?

So that's the question. I have the following variants of code in my test framework (assuming appBarButton is ApplicationBarIconButton):

var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
var method = typeof(ApplicationBarIconButton)
             .GetMethod("ClickEvent", bindingFlags);

if (method != null)
{
    method.Invoke(appBarButton, null);
}

or

IInvokeProvider invokableButton;
var isInvokable = (invokableButton = appBarButton as IInvokeProvider) != null;
if (isInvokable)
{
    invokableButton.Invoke();
}

Both pieces are not working. So I want to find some workarounds to programmatically click the ApplicationBarIconButton. Any help?

like image 866
Oleksandr Zolotarov Avatar asked Dec 19 '13 14:12

Oleksandr Zolotarov


1 Answers

UI automation, the bane of developers. For some strange reason, you cannot automate application bar buttons. But, if you can modify the application's source code, you can employ the following trick. First, create a new class:

public class CustomApplicationBarIconButton : ApplicationBarIconButton
{
    public new event EventHandler Click;

    public void RaiseClick()
    {
        if (Click != null)
            Click(this, EventArgs.Empty);
    }
}

Now, in your XAML replace shell:ApplicationBarIconButton with wp8App:CustomApplicationBarIconButton and you can now automate such buttons.

Unfortunately, you cannot programmatically replace such buttons since you cannot extract event handlers and switch them. And accessing any private methods/fields is forbidden by the platform.

like image 164
Toni Petrina Avatar answered Nov 04 '22 12:11

Toni Petrina