深入探索MVVM与WinRT组件开发
1. 命令实现与示例
首先,我们来看一个命令实现的示例。以下是IncreaseAgeCommand类的实现代码:
void IncreaseAgeCommand::Execute(Object^ parameter) { auto person = (Person^)parameter; person->BirthYear++; } bool IncreaseAgeCommand::CanExecute(Object^ parameter) { return true; }为了让这个命令起作用,我们可以创建一个命令源,比如按钮,并按以下方式填写命令细节:
<Button Content="Inrease Birth Year With Command" CommandParameter="{StaticResource p1}"> <Button.Command> <local:IncreaseAgeCommand /> </Button.Command> </Button>通常,在Command属性中创建命令并不常见,典型的做法是将其绑定到ViewModel上的适当属性。
2. MVVM 模式简介
在处理非简