Commissioned a custom control used in Smobiler

Commission (Delegate)

In C # delegate (the Delegate) pointers similar to C or C ++ function. Commission (Delegate) there is a method for a reference type variable references. Another method may be a method as a parameter to be passed.

Commission (Delegate) specifically intended for events and callback method. All commission (Delegate) are derived from System.Delegate class.

Using delegates, must meet four conditions:


Declare a delegate type;
there must be a method that contains the code to be executed;
you must create a delegate instance;
must call (invoke) delegate instance.


Declare a delegate

Commissioned by the declaration determines the method referenced by the delegate. A delegate may be directed to its method with the same label.

public delegate void MyDelegate (string a);

Delegate invocation

You must instantiate the delegate before calling.
E.g:

the delegate void MyDelegate public ();
// instantiate a delegate
the printString new new MyDelegate EX1 = ();
// call to delegate () calls Invoke, or may directly be omitted
ex1.Invoke ();

Principal applications

When using Smobiler custom control, often require custom event in a custom control, then you can apply to the commission.
Create their own custom controls can view the official website smobiler custom content.

Scenarios, there are custom button controls, need to click the button event triggers the custom control.
We look directly below, How to use:

ExampleButton class partial: Smobiler.Core.Controls.MobileUserControl
{
/// <Summary>
/// occurs when clicking the Delete button
/// </ Summary>
[the Description ( "occurs when clicking the Delete button")]

Event the EventHandler ButtonPress public;
public ExampleButton (): Base ()
{
// This Call required by IS The SmobilerUserControl.
the InitializeComponent ();
}
Private void SmobilerUserControl1_Load (SENDER Object, EventArgs E)
{
button1.Press + = (obj, args) => {this.OnButtonPress ();};
}
Private void OnButtonPress ()
{
IF (! = null ButtonPress) ButtonPress.Invoke (the this, new new EventArgs ());
}
/// <Summary>
/// a delegate click the button that represents the method to call.
/// </ Summary>
/// <param name = "SENDER"> Event Source </ param>
DeletePress /// <param name = "E"> containing event data </ param>
/// <Remarks> </ remarks>
the delegate void the EventHandler public (SENDER Object, EventArgs E);
}
can then be added in the Form View custom:

2
View custom control events, we find that the event has been added successfully:

1

Guess you like

Origin blog.51cto.com/14360220/2412576