Caliburn第3部分 事件 参数

本文详细介绍了如何在应用中使用Caliburn Micro进行事件绑定,包括长语法和短语法的使用,以及如何在事件中加入参数,同时解释了Caliburn Micro如何自动处理参数设置。

有参数的事件绑定

 长语法的优点是它的兼容性与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/

<think>好的,我现在需要回答用户关于“WPF ParameterItem”的问题。首先,我需要确定用户所指的ParameterItem在WPF中的具体含义,因为根据我的知识库,WPF本身并没有直接名为ParameterItem的内置类或结构。这可能是一个用户自定义的类,或者是第三方库中的组件,也可能是特定框架或MVVM模式中的一部分。 首先,我应该考虑用户可能遇到的上下文。例如,ParameterItem可能用于数据绑定、命令参数传递,或者在某种UI设计模式中作为数据容器。接下来,我需要查找是否有常见的WPF相关库或框架使用这个名称。比如,Prism或MVVM Light等框架中是否有类似的类。 如果ParameterItem是用户自定义的,可能需要解释其在项目中的典型用途,比如作为数据模型的一部分,存储参数信息,或者用于事件参数传递。同时,可能需要说明如何在XAML和C#代码中使用这样的类,比如数据绑定、ItemControl的数据模板等。 另外,用户可能在使用某个教程或第三方库时遇到了这个术语,需要进一步询问上下文,但由于当前无法提问,只能基于现有知识进行推测。可能需要建议用户检查他们的代码库或引用,确认ParameterItem的来源和定义。 最后,确保回答结构清晰,分点说明可能性,并建议用户提供更多上下文以便更准确的帮助。同时,遵循系统指令,正确使用LaTeX格式,虽然这个问题可能不涉及数学公式,但需注意格式要求。</think>在 WPF 中,`ParameterItem` 并不是一个原生的内置类或核心概念。根据上下文不同,它可能是以下几种情况之一: --- ### 1. **自定义数据模型类** 若在 MVVM 模式中,`ParameterItem` 可能是开发者定义的**数据模型类**,用于封装参数信息。例如: ```csharp public class ParameterItem { public string Name { get; set; } // 参数名称 public object Value { get; set; } // 参数值 public Type DataType { get; set; }// 数据类型 } ``` - **用途**:存储配置参数、动态生成UI控件(如根据数据类型渲染`TextBox`或`CheckBox`)。 - **绑定示例**: ```xml <ListBox ItemsSource="{Binding Parameters}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Name}"/> <TextBox Text="{Binding Value}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ``` --- ### 2. **命令参数传递** 在 `ICommand` 实现中,`ParameterItem` 可能用于传递操作参数: ```csharp public class RelayCommand : ICommand { private Action<ParameterItem> _execute; public void Execute(object parameter) { _execute(parameter as ParameterItem); } } ``` - **XAML 使用**: ```xml <Button Command="{Binding SaveCommand}" CommandParameter="{Binding SelectedParameter}"/> ``` --- ### 3. **第三方库或框架中的概念** 某些 WPF 扩展库可能包含 `ParameterItem`,例如: - **Prism 框架**:用于模块化参数传递。 - **动态数据绑定库**:描述数据源的元数据。 --- ### 4. **UI 控件相关** 若与 `ItemsControl` 结合使用,可能是用于生成动态项的视图模型: ```xml <ItemsControl ItemsSource="{Binding ParameterItems}"> <ItemsControl.ItemTemplate> <DataTemplate> <Label Content="{Binding Name}"/> <ContentControl Content="{Binding EditorView}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> ``` --- ### 如何进一步确认? 1. **检查代码定义**:搜索项目中 `ParameterItem` 的类定义。 2. **查看引用来源**:确认是否来自 NuGet 包(如 `Caliburn.Micro` 或 `Prism`)。 3. **分析使用场景**:观察其是否用于数据绑定、事件参数或消息传递。 如需更具体的解释,请提供更多上下文代码或应用场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值