【发布时间】:2015-05-26 02:40:22
【问题描述】:
我正在尝试在一个方法中创建一个 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 中将 Command2 绑定到 Button.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"; } }
【问题讨论】:
-
我忘了提及我的环境:这是在 Windows 8.1 WinRT 应用程序中。我正在运行 64 位 Windows 8.1(在运行 Boot Camp 的 MacBook Pro 上,如果这很重要的话)。无论我在本地还是在模拟器中运行应用程序,行为都是相同的。我今天在VS 2013中使用nuget安装了MVVM Light,GalaSoft.MvvmLight.dll的版本好像是5.1.1.35049。
标签: c# mvvm-light