I BELIEVE I CAN

加油,努力,奋斗!!

导航

The seventh

Continue silverlight about user control.

First create a LoginBox.xaml

Html:

<UserControl x:Class="LoginTest.LoginBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>   
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="UserName:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"/>
        <TextBlock Text="Password:" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"/>
        <TextBox x:Name="txtUserName" Grid.Row="0" Grid.Column="1" Margin="5"/>
        <TextBox x:Name="txtPassword" Grid.Row="1" Grid.Column="1" Margin="5"/>
        <Button x:Name="btnLogin" Grid.Row="2" Grid.Column="2" Content="Click Me"
                HorizontalAlignment="Right" Click="btnLogin_Click"/>
    </Grid>
</UserControl>

LoginBox.cs 

     public partial class LoginBox : UserControl
    {
        public event EventHandler<UserNameEventArgs> LoggedIn; // Eventhandler for do action on page
        public LoginBox()
        {
            InitializeComponent();
        }

        bool CheckPasswrod(string userName, string password)
        {
            // Check value ...
            return true;
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if(CheckPasswrod(txtUserName.Text,txtPassword.Text))
            {
                if (LoggedIn!=null)
                {

                    // Transfer parameter 'UserName'
                    LoggedIn(this, new UserNameEventArgs(){UserName = txtUserName.Text});
                }
            }
        }
    }

     // This class is for new a property of eventhandler

    public class UserNameEventArgs : EventArgs 
    {
        public string UserName { get; set; }
    }

 

Page code

Html:

<UserControl x:Class="LoginTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:local="clr-namespace:LoginTest"
    Width="400" Height="300">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <local:LoginBox LoggedIn="LoginBox_LoggedIn"></local:LoginBox>
        <TextBlock x:Name="txbMessage" Text="Not Set"/>
    </StackPanel>
</UserControl>

Page.cs

        private void LoginBox_LoggedIn(object sender, UserNameEventArgs e)
        {
            txbMessage.Text = string.Format("User {0} loggined in", e.UserName);
        }
 

We can set username in page. Like

<local:LoginBox LoggedIn="LoginBox_LoggedIn" username="Jerome"></local:LoginBox>

Also need initialize LoginBox.cs 

Add

        public LoginBox()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(LoginBox_Loaded);
        }

        public string username { get; set; }
        void LoginBox_Loaded(object sender, RoutedEventArgs e)
        {
            txtUserName.Text = username;
        }

posted on 2008-12-19 14:29  朱小能  阅读(265)  评论(0)    收藏  举报