문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top