为什么用 Command 替换 DelegateCommand 在 Prism 中不起作用

Why, replacing DelegateCommand with Command is not working in Prism

在 Prism Mvvm 中,Prism.Unity 库中,当我将 DelegateCommand 替换为 Binding Mvvm Command 时。它不工作。这是我的工作代码

public class MainPageViewModel : BindableBase
{
    private DelegateCommand _navigationCommand;

    private INavigationService _navigationService;
    public DelegateCommand NavigateCommand => _navigationCommand ?? (_navigationCommand = new DelegateCommand(ExecuteCommand));

    public MainPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
    }
    void ExecuteCommand()
    {
        _navigationService.NavigateAsync("SecondPage");
    }
}

现在我在 DeletegateCommand 中进行更改,Command 没有被触发。这是我修改后的代码

public class MainPageViewModel : BindableBase
{
    public ICommand _navigationCommand { private set; get; }
    private INavigationService _navigationService;

    public MainPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
        _navigationCommand = new Command(() => ExecuteCommand());
    }
    void ExecuteCommand()
    {
        _navigationService.NavigateAsync("SecondPage");
    }
}

嗯,我不完全确定这可能是原因,但我认为您的代码应该如下所示:

public ICommand NavigationCommand { set; get; }

然后在构造函数中设置:

 public MainPageViewModel(INavigationService navigationService)
 {
    _navigationService = navigationService;
    NavigationCommand = new Command(ExecuteCommand);
 }

您的方法如下所示:

private void ExecuteCommand(object obj) 
{
    _navigationService.NavigateAsync("SecondPage");
}

如果您想将任何数据作为命令参数传递,请使用object obj

我在 XAML 中执行命令错误,因为我遇到了这个问题。

谢谢。