Caliburn.Micro学习之Action Conventions
|
最近在学习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:
cs: public void SimpleSayHello() => Output = "Hello from Caliburn.Micro";
这种是同步方法的绑定,还有异步方法的绑定,异步绑定时UI的名称可以省略Async
Xaml:
cs:
public Task SimpleShowHelloAsync() { Output = "Hello from Caliburn.Micro"; return TaskHelper.FromResult(true); }
2.用CM的Message.Attach属性进行绑定
2.1 直接绑定方法名称SimpleSayHello
xaml:
cs: public void SimpleSayHello() => Output = "Hello from Caliburn.Micro";
2.2 用固定格式cal:Message.Attach="[Event Action] = [Action 函数名称]" 或cal:Message.Attach="[Event Action] = [函数名称]"
xaml:
cs: public void Clear() => Output = String.Empty;
如果是绑定的方法带参数,则如下
xaml:
cs: public void SayHello(string name) => Output = $"Hello {name}";
也可以同时绑定多个方法,用;分开
xaml:
cs: public void WithDoubleClick() {} public void SimpleSayHello() {}
如果是用这种方式进行异步方法的绑定,就不能省略方法名称
xaml:
cs: public void WithDoubleClick() {} public Task SimpleShowHelloAsync() { Output = "Hello from Caliburn.Micro"; return TaskHelper.FromResult(true); }
异步方法传参数
xaml:
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" >
- <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>
复制代码 来源:https://www.cnblogs.com/JaneBlogWH/p/18377968
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|
|
|
发表于 2024-8-24 19:30:54
来自手机
举报
回复
分享
|
|
|
|