简体   繁体   中英

Why, replacing DelegateCommand with Command is not working in Prism

In Prism Mvvm, Prism.Unity library, when I replace DelegateCommand with Binding Mvvm Command . 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. 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

I implement command wrong in XAML due to that I was facing this issue.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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