问题描述:

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?

网友答案:

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.

相关阅读:
Top