event handlers in assemblies

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"/>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值