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

創(chuàng)新互聯(lián)是專業(yè)的南鄭網(wǎng)站建設(shè)公司,南鄭接單;提供網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行南鄭網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
首先我們編寫(xiě)一個(gè)簡(jiǎn)單的業(yè)務(wù)類,由于數(shù)據(jù)綁定驗(yàn)證只能在雙向綁定中,所以這里需要實(shí)現(xiàn)INotifyPropertyChanged接口,如下Silverlight數(shù)據(jù)驗(yàn)證代碼所示,在set設(shè)置器中我們對(duì)于數(shù)據(jù)的合法性進(jìn)行檢查,如果不合法則拋出一個(gè)異常:
- /// < 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)); - }
- }
- }
編寫(xiě)Silverlight數(shù)據(jù)驗(yàn)證,如下代碼所示,設(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>
實(shí)現(xiàn)BindingValidationError事件,在這里可以根據(jù)ValidationErrorEventAction來(lái)判斷如何進(jìn)行處理,在界面給出相關(guān)的提示信息等,如下Silverlight數(shù)據(jù)驗(yàn)證代碼所示:
- /// < 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 = "年齡驗(yàn)證成功";
- messageAge.Validation = true;
- }
- }
通過(guò)這樣的方式,我們就可以實(shí)現(xiàn)Silverlight數(shù)據(jù)驗(yàn)證。
當(dāng)前文章:Silverlight數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)技巧分享
分享鏈接:http://www.dlmjj.cn/article/dpscech.html


咨詢
建站咨詢
