Define WPF events and Test

Source: Internet
Author: User

Custom events are often used in custom controls. In WPF, you can define events in two ways,

 
Public partial class usercontrol1: usercontrol {public static readonly routedevent okbuttonclickevent = eventmanager. registerroutedevent ("okbuttonclick", routingstrategy. bubble, typeof (routedeventhandler), typeof (usercontrol1); public event upload okbuttonclick {Add {addhandler (okbuttonclickevent, value);} remove {removehandler (okbuttonclickevent, value );}}}

Or

 
Public partial class usercontrol1: usercontrol {public event routedeventhandler cancelbuttonclick ;}

 

The events are:

 
Protected void on_btnok_click (Object sender, routedeventargs e) {raiseevent (New routedeventargs (okbuttonclickevent ));}

And

Protected void on_btncancel_click (Object sender, routedeventargs e) {If (cancelbuttonclick! = NULL) {cancelbuttonclick (sender, e );}}

Write test cases:

Public void test1 () {// This. usercontrol11.addhandler (usercontrol1.buttonclickevent, new routedeventhandler (usercontrol11_okbuttonclick); this. usercontrol11.okbuttonclick + = new routedeventhandler (usercontrol11_okbuttonclick); this. usercontrol11.cancelbuttonclick + = new routedeventhandler (usercontrol11_cancelbuttonclick); button btnok = This. usercontrol11.findname ("btnok") as button; btnok. raiseevent (New routedeventargs (button. clickevent); button btncancel = This. usercontrol11.findname ("btncancel") as button; btncancel. raiseevent (New routedeventargs (button. clickevent); this. usercontrol11.okbuttonclick-= new routedeventhandler (usercontrol11_okbuttonclick); this. usercontrol11.cancelbuttonclick-= new routedeventhandler (usercontrol11_cancelbuttonclick);} private void usercontrol11_okbuttonclick (Object sender, routedeventargs e) {console. writeline ("usercontrol11_okbuttonclick");} private void usercontrol11_cancelbuttonclick (Object sender, routedeventargs e) {console. writeline ("usercontrol11_cancelbuttonclick ");}

 

ExampleCode: Wpfcontrollibrary1.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.