內容摘要
接上一講(http://www.cnblogs.com/chenxizhang/archive/2012/04/13/2446415.html),這一講中我使用Prism做了演示和比較。Prism不僅僅是一個MVVM框架,它還包含其他的模塊。在MVVM這個層面,Prism有些特殊性(Command綁定有特殊語法),這也是我這一講的主要內容。
Prism的下載鏈接
http://compositewpf.codeplex.com/
視頻地址
http://www.tudou.com/programs/view/72Ag1kQt1RA/
示例代碼
<Window x:Class="WPFPrismMvvm.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" xmlns:local="clr-namespace:WPFPrismMvvm" xmlns:cmd="clr-namespace:Microsoft.Practices.Prism.Commands;assembly=Microsoft.Practices.Prism" Width="525"> <Window.DataContext> <local:MainWindowViewModel UserName="chenxizhang"></local:MainWindowViewModel> </Window.DataContext> <Grid> <StackPanel> <TextBox Text="{Binding UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox> <Button Content="Show" cmd:Click.Command="{Binding ShowCommand}" cmd:Click.CommandParameter="{Binding UserName}"></Button> </StackPanel> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.Prism.ViewModel; using Microsoft.Practices.Prism.Commands; using System.Windows.Input; using System.Windows; namespace WPFPrismMvvm { public class MainWindowViewModel:NotificationObject { private string _UserName; public string UserName { get { return _UserName; } set { if (_UserName != value) { _UserName = value; RaisePropertyChanged("UserName"); } } } public ICommand ShowCommand { get { return new DelegateCommand<string>( (user) => { MessageBox.Show(user); }, (user) => { return !string.IsNullOrEmpty(user); }); } } } }