简体   繁体   English

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

[英]Why, replacing DelegateCommand with Command is not working in Prism

In Prism Mvvm, Prism.Unity library, when I replace DelegateCommand with Binding Mvvm Command . 在Prism Mvvm中,当Prism.Unity库替换为Binding Mvvm Command DelegateCommand时。 It is not working. 它不起作用。 This is my working code 这是我的工作代码

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");
    }
}

Now I make changes in DeletegateCommand , Command not getting fire. 现在,我在DeletegateCommand进行了更改, Command未启动。 This is my modified code 这是我修改的代码

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");
    }
}

Well I am not completely sure that this could be the reason but I think your code should look something like this: 我不确定这可能是原因,但我认为您的代码应如下所示:

public ICommand NavigationCommand { set; get; }

Then set it in the constructor: 然后在构造函数中设置它:

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

And your method would look something like below: 您的方法如下所示:

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

use object obj if you want to pass any data as Command Parameter 如果要传递任何数据作为命令参数,请使用object obj

I implement command wrong in XAML due to that I was facing this issue. 由于遇到此问题,我在XAML中实现了错误的命令。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM