[译]5步实现Silverlight中的Command
在這里我打算分享一個十分輕量級的技巧實現Silverlight4中的Command。
Step 1 – 實現ICommand接口
第一步是新建一個類來管理Command相關的邏輯,它需要實現ICommand接口。當然,還有很多其他的方式,我在這里只介紹這種簡單有效的實現。
DelegatedCommand類實現了ICommand接口定義的CanExecute、Execute方法和CanExecuteChanged事件。
public class DelegateCommand : ICommand {Func<object, bool> canExecute;Action<object> executeAction;bool canExecuteCache;public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute) {this.executeAction = executeAction;this.canExecute = canExecute;}#region ICommand 成員public bool CanExecute(object parameter) {bool temp = canExecute(parameter);if (canExecuteCache != temp) {canExecuteCache = temp;if (CanExecuteChanged != null) {CanExecuteChanged(this, new EventArgs());}}return canExecuteCache;}public event EventHandler CanExecuteChanged;public void Execute(object parameter) {executeAction(parameter);}#endregion }Step 2?– 定義Command
在你的ViewModel中增加一個公共屬性對外提供ICommand。一般來說,這個屬性會綁定到界面的按鈕上。
public ICommand LoadProductsCommand { get; set; }Step 3 – 創建Command
在你的ViewModel的構造函數中通過第一步中創建的DelegateCommand類實例化LoadProductsCommand。
LoadProductsCommand = new DelegateCommand(LoadProducts, CanLoadProducts);Step 4 – 創建ViewModel
接下來你需要確認你的View可以和你的ViewModel通信。很多方式都可以實現這一點,這里我是將ViewModel在XAML中定義為一個靜態資源。
<UserControl.Resources><local:ProductViewModel x:Key="vm"/> </UserControl.Resources>Step 5 – 綁定到Command
向UI中添加一個按鈕然后將你ViewModel中創建的Command屬性綁定到這個按鈕上。接下來你可能需要通過設置綁定中的CommandParameter向Command傳遞參數,這里可以是一個UI元素,如TextBox。
<Button Content="Load" Width="120" Command="{Binding LoadProductsCommand}" CommandParameter="{Binding ElementName=FilterTextBox, Path=Text}" />就此5步,你的應用程序已經實現Command了。
附錄 …
ProductViewModel的全部代碼:
public class ProductViewModel : ViewModelBase {public ProductViewModel() {this.Products = new ObservableCollection<Product>();// 注意: 這里只是代碼示例 // ViewModel中不應為Model定義數據:-) // 我們可以通過調用服務得到Model所需數據. this.AllProducts = new ObservableCollection<Product>();this.AllProducts.Add(new Product { ProductId = 1, ProductName = "Apple" });this.AllProducts.Add(new Product { ProductId = 2, ProductName = "Orange" });this.AllProducts.Add(new Product { ProductId = 3, ProductName = "Banana" });this.AllProducts.Add(new Product { ProductId = 4, ProductName = "Pear" });this.AllProducts.Add(new Product { ProductId = 5, ProductName = "Grape" });this.AllProducts.Add(new Product { ProductId = 6, ProductName = "Grapefruit" });this.AllProducts.Add(new Product { ProductId = 7, ProductName = "Strawberry" });this.AllProducts.Add(new Product { ProductId = 8, ProductName = "Melon" });this.AllProducts.Add(new Product { ProductId = 9, ProductName = "Guava" });this.AllProducts.Add(new Product { ProductId = 10, ProductName = "Kiwi" });this.AllProducts.Add(new Product { ProductId = 11, ProductName = "Pineapple" });this.AllProducts.Add(new Product { ProductId = 12, ProductName = "Mango" });LoadProductsCommand = new DelegateCommand(LoadProducts, CanLoadProducts);}private void LoadProducts(object param) {string filter = param as string ?? string.Empty;this.Products.Clear();var query = from p in this.AllProductswhere p.ProductName.ToLower().StartsWith(filter.ToLower())select p;foreach (var item in query) {this.Products.Add(item);}}private bool CanLoadProducts(object param) {return true;}public ICommand LoadProductsCommand { get; set; }public ObservableCollection<Product> AllProducts { get; set; }private ObservableCollection<Product> products;public ObservableCollection<Product> Products {get {return products;}set {products = value;this.FirePropertyChanged("Product");}} } 接下來是ViewModelBase類的全部代碼,很簡單的一個類,主要用來輔助包裝PropertyChanged。 我項目中的所有ViewModel都繼承了這個類。 public abstract class ViewModelBase : INotifyPropertyChanged {public ViewModelBase() {}public event PropertyChangedEventHandler PropertyChanged;protected void FirePropertyChanged(string propertyname) {var handler = PropertyChanged;if (handler != null)handler(this, new PropertyChangedEventArgs(propertyname));}}
本文轉自紫色永恒51CTO博客,原文鏈接:http://www.cnblogs.com/024hi/archive/2010/02/23/1671683.html ,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的[译]5步实现Silverlight中的Command的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决“安装VMM过程中无法注册SPN以及
- 下一篇: 交换机定时自动备份配置文件的方法