自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Silverlight 2數(shù)據(jù)驗(yàn)證功能實(shí)現(xiàn)方法介紹

開(kāi)發(fā) 開(kāi)發(fā)工具
Silverlight 2數(shù)據(jù)驗(yàn)證是一項(xiàng)比較新的功能。我們可以通過(guò)一些相關(guān)代碼示例,來(lái)體驗(yàn)其帶來(lái)的功能體驗(yàn)。并在第一時(shí)間內(nèi)掌握這一知識(shí)。

Silverlight從出現(xiàn)至今,已經(jīng)推出了許多版本。每一個(gè)新版本的出現(xiàn)都會(huì)為程序員們帶來(lái)一些新的功能體驗(yàn)。比如,Silverlight 2數(shù)據(jù)驗(yàn)證就是一項(xiàng)比較新的支持功能。在Silverlight 2中,當(dāng)我們把數(shù)據(jù)綁定到某個(gè)UI控件的時(shí)候,該數(shù)據(jù)所具有的有效性規(guī)則也自動(dòng)被綁定到了該UI控件上。#t#

比如某個(gè)數(shù)據(jù)字段被設(shè)置為整數(shù)型,當(dāng)我們用非整數(shù)型數(shù)據(jù)對(duì)該字段進(jìn)行更新的時(shí)候就會(huì)發(fā)生錯(cuò)誤。我們就可以利用這個(gè)規(guī)則在UI中對(duì)輸入數(shù)據(jù)進(jìn)行驗(yàn)證。要做到這點(diǎn),我們只要設(shè)置兩個(gè)XAML屬性,并在所定義的事件中實(shí)現(xiàn)我們所期望的UI行為就可以了。

比如如下的XAML代碼定義了一組控件,用戶通過(guò)TextBox對(duì)數(shù)據(jù)進(jìn)行更新體驗(yàn)Silverlight 2數(shù)據(jù)驗(yàn)證的功能:

 

  1. view plaincopy to clipboardprint?  
  2. < StackPanel x:Name="dataForm">   
  3. < TextBlock Text="FirstName" Width="125" 
    FontSize="12" />   
  4. < TextBox x:Name="FirstNameTextBox" 
    IsReadOnly="False" Width="150" 
    Text="{Binding FirstName, Mode=TwoWay, 
    NotifyOnValidationError=true, 
    ValidatesOnExceptions=true}"
     
    BindingValidationError="FirstNameTextBox_
    BindingValidationError"
    />   
  5. < /StackPanel>   
  6. < StackPanel x:Name="dataForm"> 
  7. < TextBlock Text="FirstName" Width="125" 
    FontSize="12" /> 
  8. < TextBox x:Name="FirstNameTextBox" 
    IsReadOnly="False" Width="150" 
    Text="{Binding FirstName, Mode=TwoWay, 
    NotifyOnValidationError=true, Validates
    OnExceptions=true}"
     BindingValidationError=
    "FirstNameTextBox_BindingValidationError"/> 
  9. < /StackPanel>  

 

當(dāng)NotifyOnValidationError和ValidatesOnExceptions這兩個(gè)屬性都設(shè)置為true的時(shí)候,Silverlight就會(huì)在對(duì)輸入數(shù)據(jù)驗(yàn)證所綁定的規(guī)則發(fā)生錯(cuò)誤的時(shí)候觸發(fā)BindingValidationError事件,這樣我們就會(huì)在Silverlight 2數(shù)據(jù)驗(yàn)證中發(fā)生錯(cuò)誤時(shí)得到通知。

接下來(lái)我們要做的就是實(shí)現(xiàn)事件觸發(fā)時(shí)的行為,也就是我們期望的UI行為。如:

 

  1. view plaincopy to clipboardprint?  
  2. private void FirstNameTextBox_Binding
    ValidationError(object sender, 
    ValidationErrorEventArgs e)   
  3. {   
  4. if (e.Action == ValidationErrorEventAction.Added)   
  5. {   
  6. ((Control)e.OriginalSource).Background = 
    new SolidColorBrush(Colors.Red);   
  7. this.Dispatcher.BeginInvoke(()=>HtmlPage.
    Window.Alert("The input format is invalid"));   
  8. }   
  9. if (e.Action == ValidationErrorEventAction.Removed)   
  10. {   
  11. ((Control)e.OriginalSource).Background = 
    new SolidColorBrush(Colors.White);   
  12. }   
  13. }   
  14. private void FirstNameTextBox_BindingValidation
    Error(object sender, ValidationErrorEventArgs e)  
  15. {  
  16. if (e.Action == ValidationErrorEventAction.Added)  
  17. {  
  18. ((Control)e.OriginalSource).Background = 
    new SolidColorBrush(Colors.Red);  
  19. this.Dispatcher.BeginInvoke(()=>HtmlPage.
    Window.Alert("The input format is invalid"));  
  20. }  
  21. if (e.Action == ValidationErrorEventAction.Removed)  
  22. {  
  23. ((Control)e.OriginalSource).Background = 
    new SolidColorBrush(Colors.White);  
  24. }  
  25. }  

 

在上面這段Silverlight 2數(shù)據(jù)驗(yàn)證代碼中,當(dāng)錯(cuò)誤發(fā)生時(shí),控件(這里是TextBox)的背景色就會(huì)變成紅色,并利用HTML頁(yè)面的警告窗口給用戶提示信息。而當(dāng)錯(cuò)誤被更正時(shí),空間就會(huì)還原到默認(rèn)的白色。

責(zé)任編輯:曹凱 來(lái)源: CSDN
相關(guān)推薦

2009-12-30 16:19:49

Silverlight

2009-12-30 17:44:22

Silverlight

2009-12-30 15:08:04

Silverlight

2009-12-31 16:18:44

Silverlight

2009-12-30 14:44:04

Silverlight

2009-12-29 18:02:26

SilverLight

2010-01-04 17:42:50

SilverLight

2009-12-31 15:05:00

Silverlight

2009-12-30 13:59:58

Silverlight

2009-12-30 14:28:06

Silverlight

2009-12-31 10:51:55

Silverlight

2009-12-31 14:12:40

Silverlight

2009-12-30 15:42:08

Silverlight

2009-12-30 16:10:10

Silverlight

2009-12-30 16:43:47

Silverlight

2009-12-29 18:34:21

Silverlight

2010-01-04 16:50:04

Silverlight

2009-12-30 15:53:28

Silverlight

2009-12-29 18:09:00

Silverlight

2009-12-31 14:48:28

Silverlight
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)