【问题标题】:Why, replacing DelegateCommand with Command is not working in Prism为什么,用 Command 替换 DelegateCommand 在 Prism 中不起作用
【发布时间】:2019-03-08 14:05:35
【问题描述】:

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

现在我在DeletegateCommandCommand 中进行更改,但不会着火。这是我修改后的代码

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

【问题讨论】:

  • 使用 Prism.Forms 它是 MVVM 的最佳框架
  • 你能给我们指出Command的实现吗?旁注:你为什么不想使用DelegateCommand
  • @Haukinger - 感谢您的意见。我有错误的命令实现。

标签: c# xamarin mvvm xamarin.forms prism


【解决方案1】:

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

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

【讨论】:

  • @Haukinger 将其设为私有没关系,但在某些情况下人们希望公开访问它!反正我从来没有说过它现在不应该是私人的吗?
  • 命名一个不违反关注点分离或功能嫉妒的人
  • @Haukinger 我实际上同意你的观点,但你不认为在这种情况下删除set 部分将是最好的解决方案,这样就不会有违规行为
【解决方案2】:

由于我遇到了这个问题,我在 XAML 中执行了错误的命令。

谢谢。

【讨论】:

    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2022-08-09
    • 2010-11-29
    • 2014-05-24
    相关资源
    最近更新 更多