在WPF Caliburn.Micro中,可以使用ICommand接口来实现类似于RelayCommand的方法。具体来说,可以使用Caliburn.Micro的Command类或者DelegateCommand类。

使用Caliburn.Micro的Command类时,需要在ViewModel中定义一个Command类型的属性,然后在构造函数中初始化该属性,并传递一个Action或者Func作为参数。例如:

public class MyViewModel : Screen
{
    public Command MyCommand { get; private set; }

    public MyViewModel()
    {
        MyCommand = new Command(ExecuteMyCommand);
    }

    private void ExecuteMyCommand()
    {
        // Do something
    }
}

在XAML中,可以使用Caliburn.Micro的Action.Target和Action.Method属性绑定Command。例如:

<Button Content="Click me" cal:Message.Attach="[Action Target=MyCommand]"/>

使用DelegateCommand类时,需要在ViewModel中定义一个DelegateCommand类型的属性,然后在构造函数中初始化该属性,并传递一个Action或者Func作为参数。例如:

public class MyViewModel : Screen
{
    public DelegateCommand MyCommand { get; private set; }

    public MyViewModel()
    {
        MyCommand = new DelegateCommand(ExecuteMyCommand);
    }

    private void ExecuteMyCommand()
    {
        // Do something
    }
}

在XAML中,可以使用普通的CommandBinding来绑定Command。例如:

<Button Content="Click me">
    <Button.CommandBindings>
        <CommandBinding Command="{Binding MyCommand}">
            <CommandBinding.Executed>
                <cmd:CommandEventHandler/>
            </CommandBinding.Executed>
        </CommandBinding>
    </Button.CommandBindings>
</Button>
WPF CaliburnMicro和RelayCommand相似的方法

原文地址: https://www.cveoy.top/t/topic/boTa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录