您的位置:首页 > 其它

Caliburn第3部分 事件 参数

2012-10-16 16:59 190 查看
有参数的事件绑定

长语法的优点是它的兼容性与Microsoft Expression Blend中。 简短的语法是巨大的,如果你在使用设计不感兴趣,并希望保持短

The advantage of the long syntax is its compatability with Microsoft Expression Blend. The short syntax is great if you’re not interested in using a designer and want to keep things short and sweet.

长语法

View中添加

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cal=http://www.caliburnproject.org


<RepeatButton Content="Up" Margin="15" VerticalAlignment="Top">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="IncrementCount" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RepeatButton>


What we have done here is used System.Windows.Interactivity triggers to hook up an event to a method.

EventTrigger 监听事件类型 EventName

ActionMessage 事件处理方法 MethodName

加入参数的事件

public void IncrementCount(int delta)
{
Count += delta;
}


<cal:ActionMessage MethodName="IncrementCount">
<cal:Parameter Value="1" />
</cal:ActionMessage>


The Value property of the Caliburn Micro Parameter is a dependency property, which means it also supports the usual WPF data binding. This allows you to use it in all sorts of different scenarios you come across.

2.短语法

This time we only need to add the Caliburn Micro namespace, and replace the repeat button with this new one:

<RepeatButton Content="Up" Margin="15" VerticalAlignment="Top"
cal:Message.Attach="[Event Click] = [Action IncrementCount]" />


Including an event parameter using Message.Attach will look like this:

cal:Message.Attach="[Event Click] = [Action IncrementCount(1)]"


如果不显示设置参数 Caliburn会自动设置参数

If you do not explicitly specify a parameter, Caliburn Micro will look at the parameter name in the method signiture and try find any user control in the view that matches this name (ignoring the case).If a matching user control is found, an appropriate
property on the control will be used to provide the parameter.For example, if the user control is a TextBlock, the Text property value will be used as the parameter. Again, Caliburn Micro can automatically convert strings to ints and so on if necessary.

Caliburn会从当前的Controls寻找到与方法参数名相同的控件,并将控件合适的属性赋给参数。

<UniformGrid Columns="2" VerticalAlignment="Bottom">
<Slider Name="Delta" Minimum="0" Maximum="5" Margin="15" />
<Button Name="IncrementCount" Content="Increment" Margin="15" />
</UniformGrid>


在这里,我只涵盖Caliburn提供的挂接事件的支持。 其他一些主题:

设定行动目标
特殊参数值的数据绑定
行动冒泡
行动警卫参数
Setting action targets
Special parameter values for data binding
Action bubbling
Action guards with parameters

Caliburn文档:

http://caliburnmicro.codeplex.com/wikipage?title=Cheat%20Sheet&referringTitle=Documentation

http://www.mindscapehq.com/blog/index.php/2012/1/24/caliburn-micro-part-3-more-about-events-and-parameters/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: