1

我正在制作一个带有身份验证服务的 Silverlight 4.0 导航应用程序来管理用户、登录/注销等。

我在此页面上使用了 msdn 教程:http: //msdn.microsoft.com/en-us/library/ee942451%28v=vs.91%29.aspx

它工作得很好。我可以创建具有一些自定义配置文件属性的用户并登录和注销。

问题是我创建用户表单。与教程一样,我在我的服务项目中创建了一个名为 NewUser 的类,带有数据注释。这些似乎有效,就好像我尝试使用无效字段创建用户一样,创建失败,并且出现异常,并显示存在验证错误的消息。

我希望此验证适用于我的表单。在其他表单中,我只是将一个类的实例绑定到表单并将每个字段的绑定设置为validationOnException 为true 和notifyOnValidationError 为true。

我已经在创建用户表单中进行了绑定,就像在我的其他表单中一样,但它们似乎不起作用。我试图在 NewUser 类的一组属性中设置断点。看起来它根本没有被调用/使用。

这很可能是我遗漏了什么的简单问题,但我不知道是什么。这是否与 NewUser 类在我的服务项目中并且表单在客户端项目中的事实有关。我希望你们能帮助我。

我的 NewUser 类代码

public class NewUser 
{

    private string username;

    [Key]
    [Required()]
    public string UserName { get; set; }

    [Key]
    [Required()]
    [RegularExpression(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage="Invalid email. An email must use the format username@mycompany.com")]
    public string Email { get; set; }


    [Required()]
    public string Firstname { get; set; }

    [Required()]
    public string Lastname { get; set; }

    [Required()]
    public DateTime Birthsday { get; set; }

    [Required()]
    public bool SentNewsletter { get; set; }

    [Required()]
    public bool SentReminders { get; set; }



    [Required()]
    public string Password { get; set; }

    [CustomValidation(typeof(RegistrationValidator), "IsPasswordConfirmed")]        
    public string ConfirmPassword { get; set; }

    [Required()]
    public string SecurityQuestion { get; set; }

    [Required()]
    public string SecurityAnswer { get; set; }

}

我创建用户表单 xaml

<navigation:Page x:Class="OenskePortalen.Views.Pages.CreateNewUser" 
       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:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
       xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"                 
       xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
       xmlns:uc="clr-namespace:OenskePortalen.Views.Controls"
       d:DesignWidth="640" d:DesignHeight="680"
       Title="CreateNewUser Page">
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="180"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="160"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <uc:InfoBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" DisplayText="Her kan du oprette dig som bruger på ØnskePortalen" VerticalAlignment="Top"/>

    <TextBlock Grid.Row="1" Grid.Column="0" Text="Brugernavn" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="1" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <TextBox Grid.Row="1" Grid.Column="2" x:Name="txtUsername" Text="{Binding UserName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5"  HorizontalAlignment="Left"  />

    <TextBlock Grid.Row="2" Grid.Column="0" Text="Fornavn" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="2" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <TextBox Grid.Row="2" Grid.Column="2" x:Name="txtFirstname" Text="{Binding Firstname, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" HorizontalAlignment="Left"  />

    <TextBlock Grid.Row="3" Grid.Column="0" Text="Efternavn" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="3" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <TextBox Grid.Row="3" Grid.Column="2" x:Name="txtLastname"  Text="{Binding Lastname, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" HorizontalAlignment="Left" />

    <TextBlock Grid.Row="4" Grid.Column="0" Text="Email" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="4" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <TextBox Grid.Row="4" Grid.Column="2" x:Name="txtEmail" Text="{Binding Email, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" HorizontalAlignment="Left" />

    <TextBlock Grid.Row="5" Grid.Column="0" Text="Adgangskode" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="5" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <PasswordBox Grid.Row="5" Grid.Column="2" x:Name="txtPassword" Password="{Binding Password, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" />

    <TextBlock Grid.Row="6" Grid.Column="0" Text="Gentag adgangskode" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="6" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <PasswordBox Grid.Row="6" Grid.Column="2" x:Name="txtPasswordRepeat" Password="{Binding PasswordConfirm, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" />

    <TextBlock Grid.Row="7" Grid.Column="0" Text="Sikkerhedsspørgsmål" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="7" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <TextBox Grid.Row="7" Grid.Column="2" x:Name="txtPasswordQuestion" Text="{Binding SecurityQuestion, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" HorizontalAlignment="Left" />

    <TextBlock Grid.Row="8" Grid.Column="0" Text="Sikkerhedssvar" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="8" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <TextBox Grid.Row="8" Grid.Column="2" x:Name="txtPasswordAnswer" Text="{Binding SecurityAnswer, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Width="150" Margin="5" HorizontalAlignment="Left" />

    <TextBlock Grid.Row="9" Grid.Column="0" Text="Fødselsdag" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="9" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <sdk:DatePicker Grid.Row="9" Grid.Column="2" x:Name="dpBirthsday" SelectedDate="{Binding Birthsday, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"  VerticalAlignment="Center" Width="150" Margin="5" HorizontalAlignment="Left"/>

    <TextBlock Grid.Row="10" Grid.Column="0" Text="Nyhedsbrev" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="10" Grid.Column="1" Text=":" VerticalAlignment="Center"/>
    <CheckBox Grid.Row="10" Grid.Column="2" x:Name="cboxNewsletter" IsChecked="{Binding SentNewsletter, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Margin="5"/>

    <TextBlock Grid.Row="11" Grid.Column="0" Text="Påmindelser om fødselsdage" VerticalAlignment="Center"/>
    <TextBlock Grid.Row="11" Grid.Column="1" Text=":" VerticalAlignment="Center" />
    <CheckBox Grid.Row="11" Grid.Column="2" x:Name="cboxReminders" IsChecked="{Binding SentReminders, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Margin="5"/>



    <StackPanel Grid.Row="12" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Right" Orientation="Horizontal" >

        <Button x:Name="btnCreate" Style="{StaticResource GreenButton}" Background="{StaticResource GreenGradientBrush}" Height="20" Margin="5" Click="btnCreate_Click" Cursor="Hand" >
            <TextBlock Text="Opret ny bruger" Style="{StaticResource textStyleBlack}" />
        </Button>

        <Button x:Name="btnCancel" Style="{StaticResource BlueButton}" Background="{StaticResource BlueGradientBrush}" Height="20" Margin="5"  Click="btnCancel_Click" Cursor="Hand" >
            <TextBlock Text="Annuller" Style="{StaticResource textStyleBlack}" />
        </Button>
    </StackPanel>

    <input:ValidationSummary Grid.Row="13" Grid.Column="0" Grid.ColumnSpan="3" />

</Grid>

我在后面创建用户表单代码

public partial class CreateNewUser : Page
{
    private NewUser user;

    public CreateNewUser()
    {
        InitializeComponent();
        user = new NewUser();
        LayoutRoot.DataContext = user;
        dpBirthsday.SelectedDate = DateTime.Now;
        cboxNewsletter.IsChecked = true;
        cboxReminders.IsChecked = true;
    }

    private void btnCreate_Click(object sender, RoutedEventArgs e)
    {
        updateValidation();

        if (!Validation.GetHasError(LayoutRoot))
        {

            RegistrationDomainContext context = new RegistrationDomainContext();
            NewUser user = new NewUser();
            try
            {
                user.UserName = txtUsername.Text;
                user.Password = txtPassword.Password;
                user.Email = txtEmail.Text;
                user.ConfirmPassword = txtPassword.Password;
                user.SecurityQuestion = txtPasswordQuestion.Text;
                user.SecurityAnswer = txtPasswordAnswer.Text;
                user.Firstname = txtFirstname.Text;
                user.Lastname = txtLastname.Text;
                user.Birthsday = dpBirthsday.SelectedDate.GetValueOrDefault(DateTime.Now);
                user.SentNewsletter = cboxNewsletter.IsChecked.GetValueOrDefault(true);
                user.SentReminders = cboxReminders.IsChecked.GetValueOrDefault(true);

                context.NewUsers.Add(user);
                context.SubmitChanges(RegisterUser_Completed, null);
            }
            catch (Exception exc)
            {
                ErrorWindow w = new ErrorWindow(exc);
                w.Show();
            }


        } // end validation


    }


    private void RegisterUser_Completed(SubmitOperation so)
    {
        if (so.HasError)
        {

            ErrorWindow ew = new ErrorWindow(so.Error);
            ew.Show();
            so.MarkErrorAsHandled();
        }
        else
        {
            LoginParameters lp = new LoginParameters(txtUsername.Text, txtPassword.Password);
        }
    }


    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
        MainPage mp = Application.Current.RootVisual as MainPage;
        mp.contentFrame.Navigate(new Uri("/home", UriKind.Relative));
    }


    private void updateValidation()
    {
        foreach (var current in LayoutRoot.Children)
        {
            if (current is TextBox)
                (current as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();

            if (current is PasswordBox)
                (current as PasswordBox).GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();
        }
    }
}
4

1 回答 1

0

我相信您不能将数据绑定到私有字段 - 尝试将user您的代码隐藏更改为公共属性(也许看看实现 INotifyPropertyChanged)。

于 2012-12-10T02:57:37.713 回答