Caliburn.Micro學習之Action Conventions

JaneBlog發表於2024-08-24

最近在學習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>

相關文章