概述
眾所周知,在ASP.NET應用程序中,我們可以使用驗證控件進行數據輸入的驗證,遺憾的是在Silverlight中并沒有提供任何驗證控件,但Silverlight對于雙向數據綁定還是提供了一些基本的數據驗證支持,我們可以在set設置器中定義驗證規則,并對于不合法數據拋出異常,最后通過捕獲驗證錯誤事件來實現數據的驗證。
本文將介紹在Silverlight應用程序中如何進行數據驗證。
準備知識
Silverlight中如下兩種情況下,將會觸發驗證錯誤:
1.在綁定引擎中執行數據轉換時拋出異常
2.在業務實體的set設置器中拋出異常
為了在驗證出錯時能夠接收到通知,我們必須要在綁定對象上設置如下兩個屬性為true:
ValidatesOnExceptions:告訴綁定引擎當有異常發生時創建一個驗證異常
NotifyOnValidationError:告訴綁定引擎當有驗證錯誤發生或者錯誤排除時觸發BindingValidationError事件
這兩個屬性都定義在Binding類中,如下代碼所示:
BindingValidationError事件定義在FrameworkElement中,通過它可以接收到ValidationErrorEventArgs類型的參數,而在ValidationErrorEventArgs中定義了一個很重要的屬性Action,它的定義如下:
這里Added表示新增一個驗證異常,Removed表示排除了一個驗證異常。下面通過一個實例我們看一下如何使用它們進行數據的驗證。
實例
首先我們編寫一個簡單的業務類,由于數據綁定驗證只能在雙向綁定中,所以這里需要實現INotifyPropertyChanged接口,如下代碼所示,在set設置器中我們對于數據的合法性進行檢查,如果不合法則拋出一個異常:
/// <summary> /// Author:TerryLee /// http://www.cnblogs.com/Terrylee /// </summary> public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private int _age; public int Age { get { return _age; } set { if (value < 0) throw new Exception("年齡輸入不合法!"); _age = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Age")); } } } private String _name = "Terry"; public String Name { get { return _name; } set { if (value.Length < 4) throw new Exception("姓名輸入不合法!"); _name = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } public void NotifyPropertyChanged(String propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
編寫數據綁定,如下代碼所示,設置NotifyOnValidationError和ValidatesOnExceptions屬性為true,并且定義BindingValidationError事件:
<!-- http://www.cnblogs.com/Terrylee --> <StackPanel Orientation="Horizontal" Margin="10"> <TextBox x:Name="txtName" Width="200" Height="30" Text="{Binding Name,Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" BindingValidationError="txtName_BindingValidationError"> </TextBox> <my:Message x:Name="messageName"></my:Message> </StackPanel> <StackPanel Orientation="Horizontal" Margin="10"> <TextBox x:Name="txtAge" Width="200" Height="30" Text="{Binding Age,Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" BindingValidationError="txtAge_BindingValidationError"> </TextBox> <my:Message x:Name="messageAge"></my:Message> </StackPanel>
實現BindingValidationError事件,在這里可以根據ValidationErrorEventAction來判斷如何進行處理,在界面給出相關的提示信息等,如下代碼所示:
/// <summary> /// Author:TerryLee /// http://www.cnblogs.com/Terrylee /// </summary> void txtAge_BindingValidationError(object sender, ValidationErrorEventArgs e) { if (e.Action == ValidationErrorEventAction.Added) { messageAge.Text = e.Error.Exception.Message; messageAge.Validation = false; } else if (e.Action == ValidationErrorEventAction.Removed) { messageAge.Text = "年齡驗證成功"; messageAge.Validation = true; } }
通過這樣的方式,我們就可以在Silverlight中對于數據輸入做驗證了。
總結
本文介紹了如何在Silverlight應用程序進行數據驗證,希望對大家有所幫助。更多Silverlight 2的文章請參考Silverlight 2 相關文章匯總。
![]() |
不含病毒。www.avast.com |