Caliburn.Micro学习之Action Conventions

Caliburn.Micro学习之Action Conventions Conventions Caliburn. Caliburn Action Micro 学习之

最近在学习Caliburn.Micro这个框架,应用于WPF项目。相较于prism,caliburn.micro对于事件的绑定就完全不一样,它有好几种模式,借鉴于caliburn.micro的官方simple可以研究说明一下
链接:https://github.com/Caliburn-Micro/Caliburn.Micro
文档:Caliburn.Miro\Caliburn.Micro-master\samples\features,在ActionsView.xaml中用Button的Click事件依次举例。
1.直接用X:Name绑定
Xaml: <Button x:Name="SimpleSayHello" Content="Simple Say Hello" Style="{StaticResource ActionButtonStyle}"/>
cs: public void SimpleSayHello() => Output = "Hello from Caliburn.Micro";
这种是同步方法的绑定,还有异步方法的绑定,异步绑定时UI的名称可以省略Async
Xaml: <Button x:Name="SimpleShowHello" Content="Simple Say Hello" Style="{StaticResource ActionButtonStyle}"/>
cs:
public Task SimpleShowHelloAsync() { Output = "Hello from Caliburn.Micro"; return TaskHelper.FromResult(true); }
2.用CM的Message.Attach属性进行绑定
2.1 直接绑定方法名称SimpleSayHello
xaml:
<Page xmlns:cm="http://caliburnmicro.com"> <Button cm:Message.Attach="SimpleSayHello" Content="Simple Say Hello " Style="{StaticResource ActionButtonStyle}"/> </Page>
cs: public void SimpleSayHello() => Output = "Hello from Caliburn.Micro";
2.2 用固定格式cal:Message.Attach="[Event Action] = [Action 函数名称]" 或cal:Message.Attach="[Event Action] = [函数名称]"
xaml:
<Button cm:Message.Attach="[Event Click]=[Action Clear]" Content="Clear" Style="{StaticResource ActionButtonStyle}"/> <Button cm:Message.Attach="[Event Click]=[Clear]" Content="Clear" Style="{StaticResource ActionButtonStyle}"/>
cs: public void Clear() => Output = String.Empty;
如果是绑定的方法带参数,则如下
xaml:
<TextBox x:Name="Name" Margin="0,10,0,0" HorizontalAlignment="Stretch"/> <Button cm:Message.Attach="[Event Click]=[Action SayHello(Name)]" Content="Say Hello " Style="{StaticResource ActionButtonStyle}"/>
cs: public void SayHello(string name) => Output = $"Hello {name}";
也可以同时绑定多个方法,用;分开
xaml:
<Button cm:Message.Attach="[Event MouseDoubleClick] = [WithDoubleClick];[Event Click] = [SimpleSayHello]" Content="Actions" Style="{StaticResource ActionButtonStyle}"/>
cs: public void WithDoubleClick() {} public void SimpleSayHello() {}
如果是用这种方式进行异步方法的绑定,就不能省略方法名称
xaml: <Button cm:Message.Attach="[Event MouseDoubleClick] = [WithDoubleClick];[Event Click] = [SimpleShowHelloAsync]" Content="Actions with Async" Style="{StaticResource ActionButtonStyle}"/>
cs: public void WithDoubleClick() {} public Task SimpleShowHelloAsync() { Output = "Hello from Caliburn.Micro"; return TaskHelper.FromResult(true); }
异步方法传参数
xaml: <TextBox x:Name="Name" Margin="0,10,0,0" HorizontalAlignment="Stretch"/> <Button cm:Message.Attach="SayGoodbyeAsync(Name)" Content="Say Goodbye " Style="{StaticResource ActionButtonStyle}"/>
cs: public Task SayGoodbyeAsync(string name) { Output = $"Goodbye {name}"; return TaskHelper.FromResult(true); }

2.3用Interaction.Triggers实现绑定,引入 xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xaml:

点击查看代码
 <Button x:Name="FullSyntax" Content="Simple Say Hello" Style="{StaticResource ActionButtonStyle}">
         <i:Interaction.Triggers>
             <i:EventTrigger EventName="Click">
                 <cm:ActionMessage MethodName="SimpleSayHello" />
             </i:EventTrigger>
             <i:EventTrigger EventName="MouseDoubleClick">
                 <cm:ActionMessage MethodName="WithDoubleClick" />
             </i:EventTrigger>
         </i:Interaction.Triggers>
     </Button>
评论