Fazer uma PerguntaFazer uma Pergunta
 

RespondidoAutomation vs RaiseEvent

  • sábado, 10 de março de 2007 17:30Josh SmithMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    I recently blogged about how to programmatically click a Button in WPF (http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button).  I used the automation API to click the button:

    ButtonAutomationPeer peer =
      new ButtonAutomationPeer( someButton );

    IInvokeProvider invokeProv =
      peer.GetPattern( PatternInterface.Invoke )
      as IInvokeProvider;

    invokeProv.Invoke();

    Someone left a comment mentioning that you can use the RaiseEvent method to achieve the same result:

    someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

    My question is, which is better?  What's the difference between using these two approaches?  Which does the WPF team at MSFT recommend?

    Thanks for any insights.

Respostas

  • domingo, 11 de março de 2007 18:05Neil Mosafi Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    I would probably go with the Automation approach as manually raising the event does not properly simulate what happens if the button is clicked with the mouse.
    E.g what if the button binds to a command or is disabled?

Todas as Respostas

  • domingo, 11 de março de 2007 1:08Kane- Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I always thought that the difference was that the UI automation approach was for accessing UI elements that aren't inherently part of your app, while the raiseEvent approach can only be used on WPF objects.

    I got the impression that UI automation was introduced to help out with testing in the form of script / batch testing, rather than to be used as the regular method for initiating a programatically initiated event.

    Edit: Just to clarify that raiseEvent can be used on any WPF object rather than just buttons.

  • domingo, 11 de março de 2007 18:05Neil Mosafi Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    I would probably go with the Automation approach as manually raising the event does not properly simulate what happens if the button is clicked with the mouse.
    E.g what if the button binds to a command or is disabled?
  • segunda-feira, 12 de março de 2007 0:41Josh SmithMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    Neil,

    Thanks, that's a good point.

    Josh

  • segunda-feira, 12 de março de 2007 15:15sfedorov Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Raising event has a flavor of invoking object's private methods through reflection. Certainly violates contract and potentially makes object's state inconsistent - not to mention that such an invocation could be simply meaningless and/or misleading.


  • sexta-feira, 14 de março de 2008 14:40beckycatania Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    I have tried using the Automation example provided by Josh, RaiseEvent and also just calling the event handler directly.  In each of these approaches, the button does not "look" clicked on the screen - meaning the mouse over outline and background color do not changed as they do when a user actually clicks it with the mouse.  I want to initiate a button click programmatically and also have the button "look clicked" for the user.  How can I do that?  I really appreciate your help.  Here is some of my code (all of which raises the appropriate event but does not change the appearance of my button).  My button name is btnA. 

     

    Animation example code:

     

    Dim peer As New ButtonAutomationPeer(btnA)

    Dim invokeProv As IInvokeProvider = peer.GetPattern(PatternInterface.Invoke)

    invokeProv.Invoke()

     

    RaiseEvent code:

     

    btnA.RaiseEvent(New System.Windows.RoutedEventArgs(Button.ClickEvent))

     

     

    P.S. I also tried just programmatically setting the button border and background to appear clicked but since there is no window.refresh anymore the window does not repaint to achieve the simulated click.

     

    Any ideas?  Thanks!
  • sexta-feira, 28 de março de 2008 10:03Himanshu Arora Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Well you should really not expect that "clicked" behaviour which is due to your mouse hover/down/up and totally depends on the user clicking the button. And the RaiseEvent as well as Automation are just for fraction of second so I am not sure if there may be such behaviour which is quite difficult to observe.

    Another thing you can do is to animate the button when you raise the click event.

    Thanks

    --
    Himanshu
  • sexta-feira, 28 de março de 2008 18:01beckycatania Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Thank you, Himanshu.  I thought about using Animation.  I'll try that.  Thanks!

     

  • sexta-feira, 20 de março de 2009 0:44Andrey Kozyrev Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    My 2 cents: Automation is a better way because there can be some other logic in between the call and an event raise. Consider simulating the click on the menu item of the context menu. Click will close the menu, raise will not.
  • quinta-feira, 2 de abril de 2009 12:10GSyren Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I have a similar problem. When a button is clicked with the mouse, it shows the down image until the action that was initiated by the button has completed. The down image shows the user that the action is not completed yet. I would like to accomplish the same thing when initiating the button click from code. Is there no simple way to do this?
  • segunda-feira, 1 de junho de 2009 18:57LoopM Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I've used this on a RepeatButton, and unfortunately it seems that Automation does not honor the Interval property of the RepeatButton. I have to manually enforce the repeat Interval prior to calling the automation.
  • terça-feira, 7 de julho de 2009 22:11JensNewYork Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I have almost the same problem with a list box.
    I wanted to invoke the selectedvalue methode but there is only the event.
    this.lstBox.RaiseEvent(new RoutedEventArgs(ListBox.SelectedEvent)) didn't work. The selectedvalue has still the old value.
    I load my date from a database.