Silverlight的数据验证Input validation

Silverlight的数据验证Input validation,第1张

概述传统的验证方法   开发应用程序最基本的的工作内容是进行数据验证。Silverlight的应用程序也不例外。Silverlight应用一定程度上类似于Windows Form应用。其数据验证可以用Winform传统的方法,如在准备提交时的代码中逐项检查数据的合法性。例如一个窗体中有若干输入框,和一个提交按钮。输入完毕后,点击提交按钮。我们可以在提交按钮Click事件处理程序中检查每个输入框的输入合 传统的验证方法

  开发应用程序最基本的的工作内容是进行数据验证。Silverlight的应用程序也不例外。Silverlight应用一定程度上类似于windows Form应用。其数据验证可以用Winform传统的方法,如在准备提交时的代码中逐项检查数据的合法性。例如一个窗体中有若干输入框,和一个提交按钮。输入完毕后,点击提交按钮。我们可以在提交按钮Click事件处理程序中检查每个输入框的输入合法性。如果不合法,或者用一个d出窗口提示用户,或者用别的一些方式(这些方式很多的,完全取决于UI设计),再之后就将光标停到有非法值的输入框,等待用户更正。

例如下面的代码:

                      <            UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:navigation            ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"             xmlns:uriMapper="clr-namespace:System.windows.Navigation;assembly=System.windows.Controls.Navigation"              xmlns:d            ="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"              x:Class            ="SilverlightApplication1.MainPage"             mc:Ignorable="d" d:DesignWIDth="640" d:DesignHeight="480">                          <GrID Height="480" x:name="grID1" WIDth="640">                          <sdk:Label Height="19" x:name="label1" WIDth="52" Content="学生姓名" HorizontalContentAlignment="left" HorizontalAlignment="left" margin="32,113,0" VerticalAlignment="top" />                          <TextBox Height="23" x:name="textBox1" WIDth="110" HorizontalAlignment="left" margin="109,109,0" HorizontalContentAlignment="left" VerticalAlignment="top" />                          <button Content="打印" Height="24" x:name="button1" Click="button1_Click" margin="43,233,0" VerticalAlignment="top" HorizontalAlignment="left" WIDth="70" />                          <ComboBox Height="24" HorizontalAlignment="left" margin="109,147,0" x:name="comboBox1" VerticalAlignment="top" WIDth="110">                          <ComboBoxItem Content="男" />                          <ComboBoxItem Content="女" />                          </ComboBox>                          <sdk:Label Content="性别" Height="19" HorizontalAlignment="left" HorizontalContentAlignment="left" margin="32,0" x:name="label2" VerticalAlignment="top" WIDth="52" />                          <sdk:Label Height="32" HorizontalAlignment="left" margin="68,35,0" x:name="label3" VerticalAlignment="top" WIDth="141" Content="学生档案管理" FontSize="16" />                          <button Content="保存" Height="23" margin="134,0" x:name="button2" VerticalAlignment="top" Click="button2_Click" HorizontalAlignment="left" WIDth="75" />                          </GrID>                         </UserControl>          

cs代码:

                      using             System; using             System.Collections.Generic; using             System.linq; using             System.Net; using             System.windows; using             System.windows.Controls; using             System.windows.documents; using             System.windows.input; using             System.windows.Media; using             System.windows.Media.Animation; using             System.windows.Navigation; using             System.windows.Shapes; using             System.windows.Printing; namespace             SilverlightApplication1 { public partial class             MainPage : UserControl { public             MainPage() { InitializeComponent(); } voID pDoc_PrintPage(object             sender,PrintPageEventArgs e) { e.PageVisual = this            ; //throw new NotImplementedException();                          } private voID button1_Click(object             sender,RoutedEventArgs e) { Printdocument pDoc = new             Printdocument(); pDoc.PrintPage += new EventHandler<PrintPageEventArgs>            (pDoc_PrintPage); pDoc.Print("打印本页"            ); } private voID button2_Click(object             sender,RoutedEventArgs e) { if (textBox1.Text.Length == 0            ) { MessageBox.Show("学生姓名必须输入"            ); textBox1.Focus(); } } private voID grID1_Loaded(object             sender,RoutedEventArgs e) { grID1.DataContext = new             StudentInfo(); } } }          

这个验证是检查学生姓名是否输入了。如果没有输入就d出一个消息窗口,要求输入学生姓名。如图:

再之后将光标定位到学生姓名输入框。这是最传统的Winform输入验证。我们还是可以沿用。

用数据绑定的方式来验证数据

  除了传统的方法之外,微软还提供了另外一种方式来实现数据合法性的验证,即:用客户端数据绑定来验证输入的数据。下面介绍一下。通过定义实现接口INotifyPropertyChanged的数据对象,来实现数据验证。

  首先创建数据对象,这个数据对象必须有get和set。

                      using             System; using             System.ComponentModel; using             System.Net; using             System.windows; using             System.windows.Controls; using             System.windows.documents; using             System.windows.Ink; using             System.windows.input; using             System.windows.Media; using             System.windows.Media.Animation; using             System.windows.Shapes; namespace             SilverlightApplication1 { public class             StudentInfo : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate             { }; private string _name = null            ; private byte _gender = 0            ; public string             name { get              {             return             _name; } set              {             if (string            .IsNullOrEmpty(value)) throw new ArgumentException("Student name is required."            ); if (value.Length < 30            ) throw new ArgumentException("Student name is too long."            ); _name =             value; OnPropertyChanged("name"            ); } } public byte             Gender { get              {             return             _gender; } set              { _gender             =             value; OnPropertyChanged("gender"            ); } } private voID OnPropertyChanged(string             propertyname) { PropertyChanged(this,new             PropertyChangedEventArgs(propertyname)); } } }          

编译一下。保证编译成功。这对后面的只能提示有用。切记。
XAML页面修改如下:

                      <            UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:navigation            ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"             xmlns:uriMapper="clr-namespace:System.windows.Navigation;assembly=System.windows.Controls.Navigation"              xmlns:d            ="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:local="clr-namespace:SilverlightApplication1"              x:Class            ="SilverlightApplication1.MainPage"             mc:Ignorable="d" d:DesignWIDth="640" d:DesignHeight="480">                          <GrID Height="480" x:name="grID1" WIDth="640" Loaded="grID1_Loaded">                          <GrID.DataContext>                          <local:StudentInfo/>                          </GrID.DataContext>                          <sdk:Label Height="19" x:name="label1" WIDth="52" Content="学生姓名" HorizontalContentAlignment="left" HorizontalAlignment="left" margin="32,0" HorizontalContentAlignment="left" VerticalAlignment="top" Text="{Binding Mode=TwoWay,UpdateSourceTrigger=Explicit,NotifyOnValIDationError=True,ValIDatesOnExceptions=True,Path=name}" />                          <button Content="打印" Height="24" x:name="button1" Click="button1_Click" margin="43,0" x:name="button2" VerticalAlignment="top" Click="button2_Click" HorizontalAlignment="left" WIDth="75" />                          </GrID>                         </UserControl>          

  

笔者看英文的书中介绍是用Express Blend来写黄色和蓝色背景部分。笔者试了一下,在Visual Studio里面似乎没有和Blend一样的 *** 作界面。但是不想为了这个功能来装一个Blend。这里介绍一个只用Visual Studio就能实现的方法:

首先找到需要绑定的控件的容器,这里我们想把学生姓名绑定到textBox1,textBox1的容器是grID1。那么在这一行后回车:

输入<GrID.DataContext>,回车

再输入<local:,Visual Studio智能提示,我们从中选择StudentInfo,在输入/>。然后就成了:

                       <GrID.DataContext>                          <local:StudentInfo/>                          </GrID.DataContext>          

在容器添加数据上下文之后。在找到textBox1的Text属性。选”Appy Data Binding”.

Source这一页,StudentInfo已经作为DataContext,什么都不用变。点Path页,

选择name,这里没有Converter,就直接选择Options页。

Mode选TwoWay,UpdateSourceTrigger选:Explicit,勾选NotifiyOnValIDationError,ValIDatesOnExceptions,ValIDateOnNotifyDataErrors。

然后形成了上述XAML的蓝色背景部分。

 

之后再选grID1,加入grID1_Loaded处理程序。

                      private voID grID1_Loaded(object             sender,RoutedEventArgs e) { grID1.DataContext = new             StudentInfo(); }          

这是给grID1的DataContext初始化一个数据对象。

再在保存按钮的click时间处理程序中如此写:

                      private voID button2_Click(object             sender,RoutedEventArgs e) { var bindingExpression =             textBox1.GetBindingExpression(TextBox.TextProperty); bindingExpression.UpdateSource(); }          

这是将textBox1的Text属性绑定到数据对象。至此,我们可以运行此程序看看效果。因我们现在用的方法是在Set中抛出Exception。不能用DeBUG模式运行。请用DeBUG->Start without DeBUGging来启动程序。

姓名是必须输入的,试试不输入,直接点保存,姓名输入框会有一个红框:

鼠标移到其右上角的小三角处。

数据对象中抛出的Exception信息就会显示出来:

至此已经实现了一种根据数据对象绑定到Silverlight控件的数据验证方法。以上是在Set抛出例外的方法。

微软还为我们实现了一种DataAnnotation的机制。这样需要我们的工程引用System.ComponentModel.DataAnnotations.dll。还要加上:

                      using System.ComponentModel.DataAnnotations;          
如下代码:

                          [required(ErrorMessage = "请输入学生姓名"              )] public string               name { get                {               return               _name; } set                { var valIDatorContext               = new ValIDationContext(this,null,null              ); valIDatorContext.Membername = "name"              ; ValIDator.ValIDateProperty(value,valIDatorContext); _name =               value; } }            

又如加一个最长30个字符的限制:
                         

                          [required(ErrorMessage = "请输入学生姓名"              )] [StringLength(30,ErrorMessage="最长30个字符"              )] public string               name { get                {               return               _name; } set                { var valIDatorContext               = new ValIDationContext(this,valIDatorContext); _name =               value; } }            

以上是DataAnnotation方法来做输入验证。
Silverlight数据验证的安全性

  上面说了这么些数据验证方法。都是在客户端浏览器里运行的验证程序。都是不安全的。Silverlight程序就象JavaScript程序一样在客户端浏览器中运行。黑客可以解开xap包,修改Silverlight程序。这和黑客可以绕过JavaScript是一样的道理。所以基于Silverlight的系统,都必须在服务器端的WCF和web应用中加入严格的数据验证。才能真正保证安全。



  绿色通道: 好文要顶 关注我 收藏该文 与我联系

mikelij
关注 - 3
粉丝 - 68 荣誉: 推荐博客 +加关注 2 0 (请您对文章做出评价) « 博主前一篇: 32位asp.net应用程序在64位Windows上运行时的问题一例
» 博主后一篇: MS OpenXML SDK 2.0在Excel中指定位置填入数据和插入图片

posted on 2011-02-20 17:23 mikelij 阅读(1237) 评论(3) 编辑 收藏

评论 #1楼  回复 引用 查看    呵呵,看了之后受益很多啊 2011-02-24 09:20 | RealDigit         #2楼[楼主]  回复 引用 查看    @RealDigit
呵呵,对你有帮助就好! 多交流.
Let's keep up the communication!
2011-02-24 11:17 | mikelij         #3楼21678072011/8/3 15:54:38  回复 引用 查看    没有办法实现不需要鼠标移动到那个小三角上就能默认总显示出错误提示的方法。

总结

以上是内存溢出为你收集整理的Silverlight的数据验证Input validation全部内容,希望文章能够帮你解决Silverlight的数据验证Input validation所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1067620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存