C# WPF MVVM development framework Caliburn Micro common function guide ②

This is Caliburn A quick guide to the conventions and functions most commonly used in micro projects.

01 event connection

This automatically associates events on the control to methods on the ViewModel.

General agreement:

<Button x:Name="Save">

This will cause the button click event to call the "Save" method on the ViewModel.

Short syntax:

<Button cal:Message.Attach="Save">

This again causes the button's "Click" event to call the "Save" method on the ViewModel.

You can use different events like this:

<Button cal:Message.Attach="[Event MouseEnter] = [Action Save]">

You can pass different parameters to the method, as follows:

<Button cal:Message.Attach="[Event MouseEnter] = [Action Save($this)]">

$eventArgs

Pass EventArgs or input parameters to the operation. Note: for protected methods, this will be null because the trigger does not actually occur.

$dataContext

Pass the DataContext of the element to which the ActionMessage is attached. This is very useful in the main / detailed scenario. In the main / detailed scenario, ActionMessage may bubble to the parent VM, but it needs to carry the child instance of the operation to be performed.

$source

The actual framework element that triggers the ActionMessage to be sent.

$view

A view (usually a user control or window) bound to the ViewModel.

$executionContext

The execution context of the operation, which contains all the above information and more information. This is useful in advanced scenarios.

$this

The actual UI element to which the action is attached. In this case, the element itself is not passed as a parameter, but as its default attribute.

Long grammar

<UserControl x:Class="Caliburn.Micro.CheatSheet.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:cal="http://www.caliburnproject.org"> 
    <StackPanel> 
        <TextBox x:Name="Name" />
        <Button Content="Save"> 
            <i:Interaction.Triggers> 
                <i:EventTrigger EventName="Click"> 
                    <cal:ActionMessage MethodName="Save"> 
                       <cal:Parameter Value="{Binding ElementName=Name, Path=Text}" /> 
                    </cal:ActionMessage> 
                </i:EventTrigger> 
            </i:Interaction.Triggers> 
        </Button> 
    </StackPanel> 
</UserControl>

This syntax expression is friendly for Blend.

02 data binding

This will automatically bind the dependency property on the control to the property on the ViewModel.

General agreement:

<TextBox x:Name="FirstName" />

Bind the "Text" property of the TextBox to the "FirstName" property of the ViewModel.

Clear wording:

<TextBox Text="{Binding Path=FirstName, Mode=TwoWay}" />

This is the normal way to bind properties.

Event Aggregator event aggregator

The three different methods on the event aggregator are:

public interface IEventAggregator {  
    void Subscribe(object instance);  
    void Unsubscribe(object instance);  
    void Publish(object message, Action<System.Action> marshal);  
}

An event can be a simple class, for example:

public class MyEvent {
    public MyEvent(string myData) {
        this.MyData = myData;
    }

    public string MyData { get; private set; }
}

Original title: Caliburn Micro Xaml made easy

Original link: https://caliburnmicro.com/documentation/cheat-sheet

dotnet programming Encyclopedia

Added by biopv on Thu, 13 Jan 2022 02:52:25 +0200