我正在尝试使用MVVM模式制作一个简单的WPF应用程序。我写了一个实现ICommand接口的类:
public class RelayCommand : ICommand
{
private action<object> execute;
private Func<object,bool> canExecute;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(action<object> execute,Func<object,bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
}
然后,当我单击视图中的按钮时,通过将页面分配给当前页面来使用它来显示新页面
public ICommand bFirst_Click
{
get
{
return new RelayCommand(o => CurrentPage = first);
}
}
视图中的XAML代码
<StackPanel>
<Button Command="{Binding bFirst_Click}" Content="First"/>
</StackPanel>
<Frame
Grid.Column="1"
Content="{Binding CurrentPage}"
NavigationUIVisibility="Hidden"
Opacity="{Binding FrameOpacity}"
/>
但是什么也没发生。请帮助我,我错过了什么,还是做错了吗?