WPF登录跳转
新建一个登录窗口,实现登录跳转
第一步:新建wpf窗口,命名为LoginWindow
第二步:按如下方式修改LoginWindow.xaml,设计登录窗体
<Window x:Class="wpfbase.LoginWindow"
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"
xmlns:local="clr-namespace:wpfbase"
mc:Ignorable="d" Icon="F:\chenggeng\VS2015Project\wpfbase\wpfbase\source\icon\image.ico"
Title="登录" Height="320" Width="420"
ResizeMode="NoResize" Background="#ebf2f9"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="150" />
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>
<Image Source="F:\chenggeng\VS2015Project\wpfbase\wpfbase\source\image\back.jpg"
Stretch="UniformToFill" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" />
<Image Source="F:\chenggeng\VS2015Project\wpfbase\wpfbase\source\image\user.png"
Grid.Row="1" Grid.Column="0" Margin="21 5 0 0"/>
<TextBox Grid.Row="1" Grid.Column="1" Height="30" Width="180" FontSize="14" MaxLength="20"
TextBlock.LineHeight="22" TextBlock.LineStackingStrategy="BlockLineHeight"
Foreground="Black" BorderBrush="#0c0c0c" Margin="0 10 0 50" />
<PasswordBox Grid.Row="1" Grid.Column="1" Height="30" Width="180" FontSize="14" MaxLength="20"
TextBlock.LineHeight="22" TextBlock.LineStackingStrategy="BlockLineHeight"
Foreground="Black" BorderBrush="#0c0c0c" Margin="0,40,0,20"/>
<CheckBox Grid.Row="1" Grid.Column="1" Foreground="#8d9195" BorderBrush="#8d9195"
FontSize="11" TextBlock.LineHeight="16" TextBlock.LineStackingStrategy="BlockLineHeight"
Margin="0,75,0,-25">
记住密码
</CheckBox>
<CheckBox Grid.Row="1" Grid.Column="1" Foreground="#8d9195" BorderBrush="#8d9195"
FontSize="11" TextBlock.LineHeight="16" TextBlock.LineStackingStrategy="BlockLineHeight"
Margin="100,75,0,-25">
自动登录
</CheckBox>
<AccessText Grid.Row="1" Grid.Column="2" Margin="10,17,-20,0" FontSize="11"
Foreground="#2786E4" Cursor="Hand" >
注册账号
</AccessText>
<AccessText Grid.Row="1" Grid.Column="2" Margin="10,47,-20,0" FontSize="11"
Foreground="#2786E4" Cursor="Hand">
找回密码
</AccessText>
<Image Source="F:\chenggeng\VS2015Project\wpfbase\wpfbase\source\image\adduser.png"
Width="30" Height="30" Margin="10,15,80,10" Grid.Row="2" />
<Button Content="登 录" Height="30" Width="180" FontSize="12"
Background="SkyBlue" Cursor="Hand" Foreground="White"
BorderBrush="SkyBlue" Grid.Column="1" Margin="0,10" Grid.Row="2"/>
</Grid>
</Window>
第三步:在App.xaml中将启动窗体设置为LoginWindow.xaml
<Application x:Class="wpfbase.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfbase"
StartupUri="LoginWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
第四步:启动程序后,界面如下
第五步:添加登录按钮的响应函数
给帐号框和密码框添加名称,给登录按钮添加Click属性,绑定函数SignInButtonClick
<TextBox x:Name="account" Grid.Row="1" Grid.Column="1" Height="30" Width="180" FontSize="14"
MaxLength="20" TextBlock.LineHeight="22" TextBlock.LineStackingStrategy="BlockLineHeight"
Foreground="Black" BorderBrush="#0c0c0c" Margin="0 10 0 50" />
<PasswordBox x:Name="password" Grid.Row="1" Grid.Column="1" Height="30" Width="180" FontSize="14"
MaxLength="20" TextBlock.LineHeight="22" TextBlock.LineStackingStrategy="BlockLineHeight"
Foreground="Black" BorderBrush="#0c0c0c" Margin="0,40,0,20"/>
...
<Button Content="登 录" Height="30" Width="180" FontSize="12"
Background="SkyBlue" Cursor="Hand" Foreground="White"
BorderBrush="SkyBlue" Grid.Column="1" Margin="0,10" Grid.Row="2"
Click="SignIn_Click"/>
在LoginWindow.xaml.cs中添加SignIn_Click函数,LoginWindow.xaml.cs内容如下
...
namespace wpfbase
{
public partial class LoginWindow : Window
{
...
private void SignIn_Click(object sender, RoutedEventArgs e) {
string accountstr = account.Text.ToString();
IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(
password.SecurePassword);
string passwordstr = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
if("123" == accountstr && "123" == passwordstr) {
MessageBox.Show("登录成功!");
MainWindow mainwindow = new MainWindow();
mainwindow.Show();
this.Close();
}
else
MessageBox.Show("用户不存在或密码错误!");
}
}
}
这里我们设置了用户名和密码为123时能够登录,并实例化一个MainWindow,并使MainWindow显示。