技术开发 频道

Silverlight3 Beta:数据检验

  然后,我们只要在MainPage.xaml.cs文件中添加如下代码:

public MainPage()
        {
            InitializeComponent();
            
this.Loaded += OnLoaded;
        }
        
void OnLoaded(object sender, RoutedEventArgs e)
        {
            
this.DataContext = new EmailInfo("daizhj617595@126.com", "你好!", "这是邮件内容", 1);
        }

  接着,运行一下代码:

  
    
  另外,如果想要收集错误信息,以便统一进行显示的话,可以使用“OnValidationError”事件,如下:

public ObservableCollection<ValidationError> Errors { get; set; }

public MainPage()
{
    InitializeComponent();
    
this.Errors = new ObservableCollection<ValidationError>();
    
this.Loaded += OnLoaded;
}
void OnLoaded(object sender, RoutedEventArgs e)
{
    
this.DataContext = new EmailInfo("daizhj617595@126.com", "你好!", "这是邮件内容", 1);
}
private void OnValidationError(object sender, ValidationErrorEventArgs e)
{
    
if(e.Action == ValidationErrorEventAction.Added)
        Errors.Add(e.Error);
    
else if (e.Action == ValidationErrorEventAction.Removed)
        Errors.Remove(e.Error);          
}      

  这样错误信息就被添加到Errors集合中了。
    
  另外还可以通过“点击提交按钮”方式来统一进行收集,下面是一个Button的Click事件代码:

void OnSubmit(object sender, RoutedEventArgs args)
{
     List
<ValidationError> errors = new List<ValidationError>();
    
foreach (UIElement ui in LayoutRoot.Children)
     {
         FrameworkElement fe
= ui as FrameworkElement;
        
if (fe != null)
         {
            
foreach (ValidationError ve in Validation.GetErrors(fe))
             {
                 errors.Add(ve);
             }
         }
     }
}
0
相关文章