<Button x:Name="ClearTextMethod">Clear</Button>
Now, Let’s change the signature to Event Trigger based approach. First, we need to add a namespace in our XAML Headers.
xmlns:cal="http://www.caliburnproject.org"
Event Trigger Based Approach
And now we will attach the event. Notice the change in Syntax.
<Button Content="Clear" cal:Message.Attach="[Event Click] = [Action ClearTextMethod]" />
As mentioned earlier, this approach allows us to attach more events to the Control. If we need to attach a MouseOver Event to the above button, all we need to do is add another pair of [Event][Action] separated by a semi-colon
<Button Content="Clear" cal:Message.Attach="[Event Click] = [Action ClearTextMethod];[Event MouseLeave]=[Action AnotherMethod]" />
Passing Parameter
We could also pass parameters to the method, with the same syntax.
A word of caution though when you pass a boolean parameter to the method. For example, invoking the method with following syntax doesn’t quite work.
<Button Content="Clear" cal:Message.Attach="[Event Click] = [Action ClearTextMethod(true)]" />
The workaround is fairly simple thought, just include a single quote around the string representing True/False. We can modify the syntax as following.
<Button Content="Clear" cal:Message.Attach="[Event Click] = [Action ClearTextMethod('true')]" />
Complete list of tutorials on Caliburn.Micro can be found here
3 thoughts on “Caliburn Micro #003 : Events (Short Hand Syntax)”