Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top