[英]RelayCommand Execute delegate doesn't work if local variables are used
I am attempting to create an MVVM Light RelayCommand
in a method: 我试图在一个方法中创建一个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
}
If I use the viewName
parameter to the method in the Execute delegate for the RelayCommand
, it will not fire. 如果我将
viewName
参数用于RelayCommand
的Execute委托中的RelayCommand
,则不会触发它。 I am binding this command to a button. 我将此命令绑定到一个按钮。 When I click the button, not even the
Debug.WriteLine
command fires (and a breakpoint placed on it won't break). 当我单击该按钮时,甚至没有激活
Debug.WriteLine
命令(并且放置在它上面的断点不会中断)。
However, if I replace the viewName
parameter with a hard-coded string that is the same as the value in viewName
, the RelayCommand
works fine. 但是,如果我更换
viewName
用硬编码字符串,它是一样的,在值参数viewName
,该RelayCommand
工作正常。
Note that this code, where the command isn't used in a button executes without a problem: 请注意,此代码(其中未在按钮中使用该命令)执行时没有问题:
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)); }
But if I bind Command2
to Button.Command
in Xaml, it doesn't execute: 但是如果我将
Command2
绑定到Xaml中的Button.Command
,它就不会执行:
public ICommand TestCommand2 { get { return Command2("Cater"); } }
<Button Grid.Row="1" Grid.Column="1" Command="{Binding TestCommand2}" Content="TEST" />
Any ideas what might be going on here? 任何想法可能会发生在这里?
UPDATE UPDATE
Further experimentation shows that using a virtual property in the Execute delegate instead of a parameter does appear to work. 进一步的实验表明,在Execute委托中使用虚拟属性而不是参数似乎确实有效。 The command created by
NavigateToViewCommand
in this code works fine when bound to button.Command. 由
NavigateToViewCommand
在此代码中创建的命令在绑定到button.Command时工作正常。 That doesn't resolve the issue, of course; 当然,这并不能解决问题; this is just more information.
这只是更多信息。
// 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"; } }
I prefered to use in-built ICommand
pattern instead of this parameter passing to RelayCommand
ctor , which means: 我更喜欢使用内置的
ICommand
模式而不是将此参数传递给RelayCommand
ctor ,这意味着:
protected RelayCommand NavigateToViewCommand()
{
return new RelayCommand((viewName) => {
Debug.WriteLine("It fired.");
Navigation.Navigate(ServiceLocator.Current
.GetInstance<IViewLocator>()
.GetViewForNavigation(viewName.ToString()));
});
}
and call execute like this: 并像这样调用execute:
NavigateToViewCommand().Execute("David");
It's a more gentle way to pass arguments to your command. 将参数传递给命令是一种更温和的方式。 ps.: I did not try this.
ps。:我没试过。 I hope it has no typo and it's working fine.
我希望它没有错字,而且工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.