最近在学习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>
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
暂无评论内容