如果使用局部变量,则RelayCommand执行委托不起作用

时间:2015-05-26 02:40:22

标签: c# mvvm-light

我正在尝试在方法中创建MVVM Light RelayCommand

protected RelayCommand NavigateToViewCommand(string viewName) {
#if false
    return new RelayCommand(() => {
        Debug.WriteLine("It fired.");
        Navigation.Navigate(ServiceLocator.Current.GetInstance<IViewLocator>().GetViewForNavigation("StudentPage2"));
    });
#else
    return new RelayCommand(() => {
        Debug.WriteLine("It fired.");
        Navigation.Navigate(ServiceLocator.Current.GetInstance<IViewLocator>().GetViewForNavigation(viewName));
    });
#endif
}

如果我对viewName的执行委托中的方法使用RelayCommand参数,则不会触发。我将此命令绑定到一个按钮。当我单击按钮时,甚至没有Debug.WriteLine命令触发(并且放置在它上面的断点不会中断)。

但是,如果我将viewName参数替换为与viewName中的值相同的硬编码字符串,则RelayCommand可以正常工作。

请注意,此代码(按钮中未使用该命令)执行时没有问题:

void Test() {
    Command1.Execute(null);
    Command2("David").Execute(null);
}
RelayCommand Command1 { get { return new RelayCommand(() => Debug.WriteLine("cmd1 executed.")); } }
RelayCommand Command2(string msg) { return new RelayCommand(() => Debug.WriteLine("cmd2 executed: " + msg)); }

但是如果我在Xaml中绑定Command2Button.Command,它就不会执行:

public ICommand TestCommand2 { get { return Command2("Cater"); } }

<Button Grid.Row="1" Grid.Column="1" Command="{Binding TestCommand2}" Content="TEST" />

任何想法可能会在这里发生什么?

更新

进一步的实验表明,在Execute委托中使用虚拟属性而不是参数似乎确实有效。由NavigateToViewCommand在此代码中创建的命令在绑定到button.Command时工作正常。当然,这并不能解决问题;这只是更多信息。

// In base class:
protected RelayCommand NavigateToViewCommand() {
    return new RelayCommand(() => Navigation.Navigate(ServiceLocator.Current.GetInstance<IViewLocator>().GetViewForNavigation(NextPageViewName)));
}
protected virtual string NextPageViewName { get { return string.Empty; } }

// In subclass:
private ICommand m_nextPage;
public ICommand NextPageCommand { get { return m_nextPage ?? (m_nextPage = NavigateToViewCommand()); } }
protected override string NextPageViewName { get { return "StudentPage2"; } }

1 个答案:

答案 0 :(得分:0)

我更喜欢使用内置 ICommand模式,而不是将此参数传递给RelayCommand ctor ,这意味着:

protected RelayCommand NavigateToViewCommand() 
{
    return new RelayCommand((viewName) => {
        Debug.WriteLine("It fired.");
        Navigation.Navigate(ServiceLocator.Current
                  .GetInstance<IViewLocator>()
                  .GetViewForNavigation(viewName.ToString()));
    });
}

并像这样调用execute:

NavigateToViewCommand().Execute("David");

将参数传递给命令是一种更温和的方式。 ps。:我没试过。我希望它没有拼写错误,并且工作正常。