Caliburn第3部分 事件 參數

有參數的事件綁定

 長語法的優點是它的兼容性與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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章