event handlers in assemblies

来源:互联网 发布:国内外电视直播软件apk 编辑:程序博客网 时间:2024/05/01 00:22
When an event is handled by a pre-compiled assembly, you can use the short form of specifying just the event handler's method name if the method name is unique. MyXaml searches all object targets that have been registered with the parser using Add Target.

 

Passing An Event Target When Loading A Form

 

A typical usage is as follows:

 

public class Startup{  public void Start()  {    Parser p=new Parser();    Form form=(Form)p.LoadForm("app.myxaml", "AppMainForm", this, null);    Application.Run(form);  }  public void OnClick(object sender, EventArgs e)  {    // ... event handler  }}

 

In the above example, an event target is automatically added to the parser's event target collection because the Startup class is specified in the third parameter ("this") when loading the form. In the markup, a typical usage would be:

 

<Button Click="OnClick"/>

 

Pre-registering Event Targets

 

A slightly more complicated example illustrates pre-registering an event target:

 

public class Startup{  public void Start()  {    MyOtherClass c=new MyOtherClass();    Parser p=new Parser();    p.AddTarget(c);    Form form=(Form)p.LoadForm("app.myxaml", "AppMainForm", this, null);    Application.Run(form);  }  public void OnClick(object sender, EventArgs e)  {    // ... event handler  }}public class MyOtherClass{  public void OnDoubleClick(object sender, EventArs e)  {     // ... event handler  }}

 

In the markup, a typical usage would be:

 

<Button Click="OnClick" DoubleClick="OnDoubleClick"/>

 

In the above example, the parser inspects both targets to resolve the event handler.

 

Specifying A Handler In A Specific Instance

 

For complex programs, you will usually find yourself wanting to specify the instance that is to handle the event. The format for this markup is:

 

<Button Click="{MyInstance.OnClick}"/>

 

IMPORTANT: The instance "MyInstance" is resolved by inspecting the definition list not the event target list.

 

You can either use Add Reference to specify the definition in the imperative code:

 

public class Startup{  public void Start()  {    MyOtherClass c=new MyOtherClass();    Parser p=new Parser();    p.AddReference("MyInstance", c);    Form form=(Form)p.LoadForm("app.myxaml", "AppMainForm", this, null);    Application.Run(form);  }}...

 

or isntantiate the class declaratively using the def:Name construct to add the instance to the definition lis:

 

<MyOtherClass def:Name="MyInstance"/><Button Click="{MyInstance.OnClick}"/>

 

Late binding is supported using this form of event wire-ups. For example, the following markup works equally well:

 

<Button Click="{MyInstance.OnClick}"/><MyOtherClass def:Name="MyInstance"/> 
原创粉丝点击