新聞中心
Silverlight開發(fā)工具對于編程人員來說是一個非常有用的多媒體處理平臺。其中有很多功能與應(yīng)用技巧值得我們?nèi)ド钊氲难芯?。在這里我們就為大家介紹一下有關(guān)Silverlight數(shù)據(jù)驗證的實現(xiàn)方法。#t#

創(chuàng)新互聯(lián)是專業(yè)的南鄭網(wǎng)站建設(shè)公司,南鄭接單;提供網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行南鄭網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
首先我們編寫一個簡單的業(yè)務(wù)類,由于數(shù)據(jù)綁定驗證只能在雙向綁定中,所以這里需要實現(xiàn)INotifyPropertyChanged接口,如下Silverlight數(shù)據(jù)驗證代碼所示,在set設(shè)置器中我們對于數(shù)據(jù)的合法性進行檢查,如果不合法則拋出一個異常:
- /// < summary>
- /// Author:TerryLee
- /// http://www.cnblogs.com/Terrylee
- /// < /summary>
- public class Person :
INotifyPropertyChanged - {
- public event PropertyChanged
EventHandler 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 Property
ChangedEventArgs(propertyName)); - }
- }
- }
編寫Silverlight數(shù)據(jù)驗證,如下代碼所示,設(shè)置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>
實現(xiàn)BindingValidationError事件,在這里可以根據(jù)ValidationErrorEventAction來判斷如何進行處理,在界面給出相關(guān)的提示信息等,如下Silverlight數(shù)據(jù)驗證代碼所示:
- /// < summary>
- /// Author:TerryLee
- /// http://www.cnblogs.com/Terrylee
- /// < /summary>
- void txtAge_BindingValidationError
(object sender, ValidationErrorEventArgs e)- {
- if (e.Action == ValidationError
EventAction.Added)- {
- messageAge.Text = e.Error.
Exception.Message;- messageAge.Validation = false;
- }
- else if (e.Action == Validation
ErrorEventAction.Removed)- {
- messageAge.Text = "年齡驗證成功";
- messageAge.Validation = true;
- }
- }
通過這樣的方式,我們就可以實現(xiàn)Silverlight數(shù)據(jù)驗證。
網(wǎng)站名稱:Silverlight數(shù)據(jù)驗證實現(xiàn)技巧分享
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/dpscech.html


咨詢
建站咨詢
