使用 wcf 服务参考类在 silverlight 中进行数据验证

发布于 2024-10-11 11:46:04 字数 375 浏览 2 评论 0原文

你好, 我将 wcf 服务silverlight 一起使用,我的数据契约类在参考文件中公开,并将类类型的集合绑定到数据网格,然后单击特定行来编辑整个行数据将绑定到数据表单,我必须验证字段,这里使用 IDataErrorInfo 接口进行验证,在服务参考文件中,该类是一个部分类并创建了一个新的 cs 文件具有相同的服务文件命名空间和类名,然后实现 IDataErrorInfo 接口属性

公共字符串 this[字符串 列名称]{}

在我编写验证的范围内。但它不起作用,任何人都可以为此提供帮助。 谢谢

HI,
Am using wcf service with silverlight and my datacontract class is exposed in the reference file and am binding a collection of class type to datagrid and while clicking a particular row for editing the entire row data will be binded to a dataform and there i have to validate the fields and here am using a IDataErrorInfo interface to validte,Here in service reference file the class is a partial class and created a new cs file with same namespace of the servicefile and class name then implemented the IDataErrorInfo interface properties

public string this[string
columnName]{}

in the scope i worte the validation .But it is not working can anyone help for this.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2024-10-18 11:46:04

使用 Silverlight 4 和 WCF 时,RequiredAttribute 和 DisplayAttribute 等数据注释不会传播到生成的客户端文件。
这是该问题的一种解决方案......
创建带有模型的类库(Model.dll)
示例


public partial class Person: INotifyPropertyChanged 
{ 
    private Guid IDField; 

    private string NameField; 

    private string LastNameField; 

    private int AgeField; 

    private string EmailField; 

    /// <summary> 
    /// ID of an Object 
    /// </summary>         
    public Guid ID 
    { 
        get 
        { 
            return IDField; 
        } 
        set 
        { 
            if (value != IDField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "ID" }); 
                IDField = value; 
                OnPropertyChanged("ID"); 
            } 
        } 
    } 

    /// <summary> 
    /// Name of a person 
    /// </summary> 
    [Required]         
    public string Name 
    { 
        get 
        { 
            return NameField; 
        } 
        set 
        { 
            if (value != NameField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" }); 
                NameField = value; 
                OnPropertyChanged("Name"); 
            } 
        } 
    } 

    /// <summary> 
    /// LastName of a person 
    /// </summary> 
    [Required] 
    public string LastName 
    { 
        get 
        { 
            return LastNameField; 
        } 
        set 
        { 
            if (value != LastNameField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); 
                LastNameField = value; 
                OnPropertyChanged("LastName"); 
            } 
        } 
    } 

    /// <summary> 
    /// Age of a person 
    /// </summary> 
    [Range(0,120)] 
    //[Required] 
    public int Age 
    { 
        get 
        { 
            return AgeField; 
        } 
        set 
        { 
            if (value != AgeField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" }); 
                AgeField = value; 
                OnPropertyChanged("Age"); 
            } 
        } 
    } 

    [RegularBLOCKED EXPRESSION] 
    public string Email 
    { 
        get 
        { 
            return EmailField; 
        } 
        set 
        { 
            if (value != EmailField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Email" }); 
                EmailField = value; 
                OnPropertyChanged("Email"); 
            } 
        } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// Raises a property changed notification for the specified property name. 
    /// </summary> 
    /// <param name="propName">The name of the property that changed.</param> 
    protected virtual void OnPropertyChanged(string propName) 
    { 
        if (PropertyChanged != null) 
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
        } 
    } 
} 

在服务项目中引用此类库
服务示例可以是...

[ServiceContract] 
    public interface IPersonService 
    { 
        [OperationContract] 
        List<Person> GetPersons(); 

        [OperationContract] 
        Person GetPersonByID(Guid ID); 

        [OperationContract] 
        void EditPerson(Person PersonField);         

    } 


///////////////// 



public class PersonService : IPersonService 
    { 
        private List<Person> Persons; 

        public PersonService() 
        { 
            Persons = new List<Person>(); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Albert", LastName = "Pujols" , Age = 31, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Alex", LastName = "Rodriguez", Age = 36, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Evan", LastName = "Longoria", Age = 25, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Joey", LastName = "Votto", Age = 25, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Miguel", LastName = "Cabrera", Age = 27, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Kendry", LastName = "Morales", Age = 26, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Alexei", LastName = "Ramirez", Age = 28, Email = "[email protected]" }); 
        } 


        public List<Person> GetPersons() 
        { 
            return Persons; 
        } 

        public Person GetPersonByID(Guid ID) 
        { 
            return (from sel in Persons where sel.ID == ID select sel).First(); 
        } 

        public void EditPerson(Person PersonField) 
        { 
            Person Person = (from sel in Persons where sel.ID == PersonField.ID select sel).First(); 
            Person = PersonField; 
        }        

    }

创建一个 silverlight 类库 (SL.Model.dll)(该库具有在 Model.dll 中创建的示例模型)
在这个类库中添加现有项目作为链接并添加具有 Model.dll 的模型项目
在 Silverlight 应用程序中引用 SL.Model.dll
当服务引用创建视图时,检查重用类型(这是默认选项)
页面示例...
xaml代码...

<UserControl x:Class="SampleApp.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
             xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
        <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="55*" /> 
            <ColumnDefinition Width="45*" /> 
        </Grid.ColumnDefinitions> 
        <sdk:DataGrid Name="dataGrid1" Grid.Column="0" IsReadOnly="True" AutoGenerateColumns="False" SelectionChanged="dataGrid1_SelectionChanged" FontSize="12"> 
            <sdk:DataGrid.Columns> 
                <sdk:DataGridTextColumn CanUserSort="True" Binding="{Binding ID}" Visibility="Collapsed" /> 
                <sdk:DataGridTextColumn CanUserSort="True" Binding="{Binding Name}" Header="Name" Width="100" /> 
                <sdk:DataGridTextColumn CanUserSort="True" Binding="{Binding LastName}" Header="LastName" Width="100" />                 
            </sdk:DataGrid.Columns> 
        </sdk:DataGrid> 
        <Grid Grid.Column="1"> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="40*" /> 
                <ColumnDefinition Width="50*" /> 
                <ColumnDefinition Width="10*" /> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="150"/> 
            </Grid.RowDefinitions> 
            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="Name" Grid.Row="0" Margin="4" /> 
            <TextBox x:Name="Name" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="0" Grid.Column="1" Margin="4" /> 

            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="LastName" Grid.Row="1" Margin="4" /> 
            <TextBox x:Name="LName" Text="{Binding LastName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="1" Grid.Column="1" Margin="4" /> 

            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="Age" Grid.Row="2" Margin="4" /> 
            <TextBox x:Name="Age" Text="{Binding Age, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="2" Grid.Column="1" Margin="4" /> 

            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="Email" Grid.Row="3" Margin="4" /> 
            <TextBox x:Name="Email" Text="{Binding Email, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="3" Grid.Column="1" Margin="4" /> 

            <dataInput:ValidationSummary Margin="4" Grid.Row="4" Grid.ColumnSpan="3" /> 


        </Grid> 



    </Grid> 
</UserControl>

控制的

public partial class MainPage : UserControl 
    { 
        private PersonServiceClient Client; 
        private EndpointAddress AddressService = new EndpointAddress(new Uri("http://localhost:3589/PersonService.svc")); 

        public MainPage() 
        { 
            InitializeComponent(); 

            Client = new PersonServiceClient(new BasicHttpBinding(), AddressService); 
            Client.GetPersonsAsync(); 
            Client.GetPersonsCompleted += new EventHandler<GetPersonsCompletedEventArgs>(Client_GetPersonsCompleted); 
        } 

        void Client_GetPersonsCompleted(object sender, GetPersonsCompletedEventArgs e) 
        { 
            ObservableCollection<Person> PersonItems = e.Result; 
            dataGrid1.ItemsSource = PersonItems;             
        } 

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
        { 
            DataContext = e.AddedItems[0]; 
        } 
    }

data annotations like RequiredAttribute and DisplayAttribute etc do not propagated to the generated client side files when using Silverlight 4 and WCF.
This is one solution for the problem...
Create a class library with a model (Model.dll)
sample


public partial class Person: INotifyPropertyChanged 
{ 
    private Guid IDField; 

    private string NameField; 

    private string LastNameField; 

    private int AgeField; 

    private string EmailField; 

    /// <summary> 
    /// ID of an Object 
    /// </summary>         
    public Guid ID 
    { 
        get 
        { 
            return IDField; 
        } 
        set 
        { 
            if (value != IDField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "ID" }); 
                IDField = value; 
                OnPropertyChanged("ID"); 
            } 
        } 
    } 

    /// <summary> 
    /// Name of a person 
    /// </summary> 
    [Required]         
    public string Name 
    { 
        get 
        { 
            return NameField; 
        } 
        set 
        { 
            if (value != NameField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" }); 
                NameField = value; 
                OnPropertyChanged("Name"); 
            } 
        } 
    } 

    /// <summary> 
    /// LastName of a person 
    /// </summary> 
    [Required] 
    public string LastName 
    { 
        get 
        { 
            return LastNameField; 
        } 
        set 
        { 
            if (value != LastNameField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); 
                LastNameField = value; 
                OnPropertyChanged("LastName"); 
            } 
        } 
    } 

    /// <summary> 
    /// Age of a person 
    /// </summary> 
    [Range(0,120)] 
    //[Required] 
    public int Age 
    { 
        get 
        { 
            return AgeField; 
        } 
        set 
        { 
            if (value != AgeField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" }); 
                AgeField = value; 
                OnPropertyChanged("Age"); 
            } 
        } 
    } 

    [RegularBLOCKED EXPRESSION] 
    public string Email 
    { 
        get 
        { 
            return EmailField; 
        } 
        set 
        { 
            if (value != EmailField) 
            { 
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Email" }); 
                EmailField = value; 
                OnPropertyChanged("Email"); 
            } 
        } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// Raises a property changed notification for the specified property name. 
    /// </summary> 
    /// <param name="propName">The name of the property that changed.</param> 
    protected virtual void OnPropertyChanged(string propName) 
    { 
        if (PropertyChanged != null) 
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
        } 
    } 
} 

In a services project reference this class library
a sample of services could be ...

[ServiceContract] 
    public interface IPersonService 
    { 
        [OperationContract] 
        List<Person> GetPersons(); 

        [OperationContract] 
        Person GetPersonByID(Guid ID); 

        [OperationContract] 
        void EditPerson(Person PersonField);         

    } 


///////////////// 



public class PersonService : IPersonService 
    { 
        private List<Person> Persons; 

        public PersonService() 
        { 
            Persons = new List<Person>(); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Albert", LastName = "Pujols" , Age = 31, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Alex", LastName = "Rodriguez", Age = 36, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Evan", LastName = "Longoria", Age = 25, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Joey", LastName = "Votto", Age = 25, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Miguel", LastName = "Cabrera", Age = 27, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Kendry", LastName = "Morales", Age = 26, Email = "[email protected]" }); 
            Persons.Add(new Person { ID = Guid.NewGuid(), Name = "Alexei", LastName = "Ramirez", Age = 28, Email = "[email protected]" }); 
        } 


        public List<Person> GetPersons() 
        { 
            return Persons; 
        } 

        public Person GetPersonByID(Guid ID) 
        { 
            return (from sel in Persons where sel.ID == ID select sel).First(); 
        } 

        public void EditPerson(Person PersonField) 
        { 
            Person Person = (from sel in Persons where sel.ID == PersonField.ID select sel).First(); 
            Person = PersonField; 
        }        

    }

Create a silverlight class library (SL.Model.dll) (this library have the sample model created in Model.dll)
in this class library add existing Item as link and add the model item that have Model.dll
in Silverlight Application reference the SL.Model.dll
when the services reference is create view that reuse types is cheked (It is deafault option)
sample of page ...
xaml

<UserControl x:Class="SampleApp.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
             xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
        <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="55*" /> 
            <ColumnDefinition Width="45*" /> 
        </Grid.ColumnDefinitions> 
        <sdk:DataGrid Name="dataGrid1" Grid.Column="0" IsReadOnly="True" AutoGenerateColumns="False" SelectionChanged="dataGrid1_SelectionChanged" FontSize="12"> 
            <sdk:DataGrid.Columns> 
                <sdk:DataGridTextColumn CanUserSort="True" Binding="{Binding ID}" Visibility="Collapsed" /> 
                <sdk:DataGridTextColumn CanUserSort="True" Binding="{Binding Name}" Header="Name" Width="100" /> 
                <sdk:DataGridTextColumn CanUserSort="True" Binding="{Binding LastName}" Header="LastName" Width="100" />                 
            </sdk:DataGrid.Columns> 
        </sdk:DataGrid> 
        <Grid Grid.Column="1"> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="40*" /> 
                <ColumnDefinition Width="50*" /> 
                <ColumnDefinition Width="10*" /> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="150"/> 
            </Grid.RowDefinitions> 
            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="Name" Grid.Row="0" Margin="4" /> 
            <TextBox x:Name="Name" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="0" Grid.Column="1" Margin="4" /> 

            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="LastName" Grid.Row="1" Margin="4" /> 
            <TextBox x:Name="LName" Text="{Binding LastName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="1" Grid.Column="1" Margin="4" /> 

            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="Age" Grid.Row="2" Margin="4" /> 
            <TextBox x:Name="Age" Text="{Binding Age, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="2" Grid.Column="1" Margin="4" /> 

            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="Email" Grid.Row="3" Margin="4" /> 
            <TextBox x:Name="Email" Text="{Binding Email, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Grid.Row="3" Grid.Column="1" Margin="4" /> 

            <dataInput:ValidationSummary Margin="4" Grid.Row="4" Grid.ColumnSpan="3" /> 


        </Grid> 



    </Grid> 
</UserControl>

code of control ...

public partial class MainPage : UserControl 
    { 
        private PersonServiceClient Client; 
        private EndpointAddress AddressService = new EndpointAddress(new Uri("http://localhost:3589/PersonService.svc")); 

        public MainPage() 
        { 
            InitializeComponent(); 

            Client = new PersonServiceClient(new BasicHttpBinding(), AddressService); 
            Client.GetPersonsAsync(); 
            Client.GetPersonsCompleted += new EventHandler<GetPersonsCompletedEventArgs>(Client_GetPersonsCompleted); 
        } 

        void Client_GetPersonsCompleted(object sender, GetPersonsCompletedEventArgs e) 
        { 
            ObservableCollection<Person> PersonItems = e.Result; 
            dataGrid1.ItemsSource = PersonItems;             
        } 

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
        { 
            DataContext = e.AddedItems[0]; 
        } 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文