在Silverlight3中对数据进行校验不再像Silverlight2中那样麻烦了,下面就简要演示一下。
首先,我们创建一个Silverlight3应用,名为:"ValIDateSample"
然后将下面的xaml代码复制到"MainPage.xaml"中:
< GrID.RowDeFinitions >
< RowDeFinition Height ="50" />
< RowDeFinition Height ="50" />
< RowDeFinition Height ="150" />
< RowDeFinition Height ="50" />
</ GrID.RowDeFinitions >
< GrID.ColumnDeFinitions >
< ColumnDeFinition WIDth ="50" ></ ColumnDeFinition >
< ColumnDeFinition WIDth ="300" ></ ColumnDeFinition >
</ GrID.ColumnDeFinitions >
< TextBlock Text ="地址:" GrID.Row ="0" GrID.Column ="0" VerticalAlignment ="Center" HorizontalAlignment ="Right" />
< TextBox margin ="5" name ="Address" VerticalAlignment ="Center" WIDth ="192" GrID.Row ="0" GrID.Column ="1"
Text =" {Binding Address,Mode=TwoWay,ValIDatesOnExceptions=True,NotifyOnValIDationError=True} " />
< TextBlock Text ="标题:" GrID.Row ="1" GrID.Column ="0" VerticalAlignment ="Center" HorizontalAlignment ="Right" />
< StackPanel GrID.Row ="1" GrID.Column ="1" OrIEntation ="Horizontal" HorizontalAlignment ="Right" >
< TextBox margin ="5" name ="Title" VerticalAlignment ="Center" GrID.Row ="1" GrID.Column ="1" WIDth ="160"
Text =" {Binding Title,NotifyOnValIDationError=True} " />
< ComboBox name ="EmailFormat" WIDth ="80" Height ="20" Selectedindex =" {Binding Format,NotifyOnValIDationError=True} " >
< ComboBoxItem Content ="邮件格式" ></ ComboBoxItem >
< ComboBoxItem Content ="HTML" ></ ComboBoxItem >
< ComboBoxItem Content ="Text" ></ ComboBoxItem >
</ ComboBox >
</ StackPanel >
< TextBlock Text ="内容:" GrID.Row ="2" GrID.Column ="0" VerticalAlignment ="Center" HorizontalAlignment ="Right" />
< TextBox margin ="5" name ="Body" GrID.Row ="2" GrID.Column ="1" WIDth ="230" Height ="130"
Text =" {Binding Body,NotifyOnValIDationError=True} " />
< button GrID.Row ="3" GrID.Column ="1" Content =" 提 交 " WIDth ="100" Height ="30" FontSize ="16" />
<!-- Click="Onsubmit" -->
</ GrID >
然后就是相应的校验信息类,本示例使用Email校验,相应的CS代码:
public class Emailinfo{
string address;
string Title;
string body;
int format;
public Emailinfo( string address, string Title, string body, int format)
{
this .address = address;
this .Title = Title;
this .body = body;
this .format = format;
}
public string Address
{
get { return address; }
set
{
CheckFIEldLength(value, 1 , 100 );
if ( ! System.Text.RegularExpressions.Regex.IsMatch(value, @" ^[/w/.]+@[A-Za-z0-9-_]+[/.][A-Za-z0-9-_] " ))
throw new Exception( " 邮件格式无效! " );
address = value;
}
}
public string Title
{
get { return Title; }
set
{
CheckFIEldLength(value, 100 );
Title = value;
}
}
voID CheckFIEldLength( string value, int min, int max)
{
if ((value.Length < min) || (value.Length > max))
throw new Exception( " 内容长度应在: " + min + " 至 " + max);
}
public string Body
{
get { return body; }
set
{
CheckFIEldLength(value, 100 );
body = value;
}
}
public int Format
{
get { return format; }
set
{
if (value == 0 )
throw new Exception( " 请选择相应的邮件格式! " );
format = value;
}
}
}
然后,我们只要在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事件代码:
详情参见这篇Blog :
{
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);
}
}
}
}
在结束今天的内容之前,再推荐一个不错的网站:http://playboy.covertocover.com/
这是大名鼎鼎的“花花公子”杂志使用Silverlight开发的应用,相信很多程序员(男性)的收藏夹
又会多一个链接了,呵呵:)
好了,今天的内容就先到这里了。源码下载,请点击这里。
原文链接:http://www.cnblogs.com/daizhj/archive/2009/04/02/1428151.html
作者: daizhj,代震军
Tags: silverlight,3 beta,data-valIDate,数据校验
网址: http://daizhj.cnblogs.com/
总结以上是内存溢出为你收集整理的快速浏览Silverlight3 Beta:数据检验全部内容,希望文章能够帮你解决快速浏览Silverlight3 Beta:数据检验所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)