在C#?WPF中實(shí)現(xiàn)登錄頁(yè)面跳轉(zhuǎn)的兩種方案
引言
在 C# WPF 中實(shí)現(xiàn)登錄頁(yè)面跳轉(zhuǎn),核心是 “驗(yàn)證登錄邏輯” 與 “頁(yè)面切換” 結(jié)合,常用兩種方案:NavigationWindow 導(dǎo)航跳轉(zhuǎn)(適合多頁(yè)面場(chǎng)景)和Window+UserControl 切換(適合單窗口集成場(chǎng)景)。以下是具體實(shí)現(xiàn)步驟:
一、基礎(chǔ)準(zhǔn)備:創(chuàng)建登錄頁(yè)面結(jié)構(gòu)
無論哪種方案,先創(chuàng)建登錄頁(yè)面(包含賬號(hào)、密碼輸入框和登錄按鈕),這里以通用 XAML 結(jié)構(gòu)為例:
登錄頁(yè)面核心 XAML(LoginPage.xaml/ LoginUC.xaml)
<!-- 以Page為例,UserControl結(jié)構(gòu)完全一致,僅根標(biāo)簽改為<UserControl> -->
<Page x:Class="WpfLoginDemo.LoginPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="登錄頁(yè)面">
<!-- 用Grid布局登錄表單,居中顯示 -->
<Grid Background="#F5F5F5">
<Grid Width="300" Height="250" Background="White" Margin="0,0,0,100" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
?
<!-- 標(biāo)題 -->
<TextBlock Grid.Row="0" Text="用戶登錄" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold"/>
<!-- 賬號(hào)輸入框 -->
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="20,0">
<TextBlock Text="賬號(hào):" Width="60" VerticalAlignment="Center"/>
<TextBox x:Name="TxtAccount" Width="180" Height="30" Margin="5,0" Padding="5"/>
</StackPanel>
<!-- 密碼輸入框 -->
<StackPanel Grid.Row="2" Orientation="Horizontal" Margin="20,0">
<TextBlock Text="密碼:" Width="60" VerticalAlignment="Center"/>
<PasswordBox x:Name="TxtPwd" Width="180" Height="30" Margin="5,0" Padding="5"/>
</StackPanel>
<!-- 登錄按鈕 -->
<Button Grid.Row="3" Content="登錄" Width="100" Height="35" Margin="0,10"
Background="#4A86E8" Foreground="White" Click="BtnLogin_Click"/>
</Grid>
</Grid>
</Page>二、方案 1:NavigationWindow 導(dǎo)航跳轉(zhuǎn)(多頁(yè)面場(chǎng)景)
適合需要獨(dú)立登錄頁(yè)、且登錄后跳轉(zhuǎn)到其他 Page(如首頁(yè))的場(chǎng)景,自帶導(dǎo)航歷史(但登錄頁(yè)通常無需后退,可隱藏導(dǎo)航欄)。
步驟 1:設(shè)置啟動(dòng)窗口為 NavigationWindow
修改App.xaml,指定啟動(dòng)窗口為NavigationWindow,并默認(rèn)加載登錄頁(yè):
<Application x:Class="WpfLoginDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainNavWindow.xaml"> <!-- 啟動(dòng)導(dǎo)航窗口 -->
</Application>步驟 2:創(chuàng)建 NavigationWindow 并隱藏導(dǎo)航欄
新建MainNavWindow.xaml,隱藏默認(rèn)導(dǎo)航欄(避免登錄后可返回登錄頁(yè)):
<NavigationWindow x:Class="WpfLoginDemo.MainNavWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF登錄示例" Width="800" Height="600"
Source="LoginPage.xaml" <!-- 初始加載登錄頁(yè) -->
ShowsNavigationUI="False"> <!-- 隱藏導(dǎo)航欄(關(guān)鍵) -->
</NavigationWindow>步驟 3:實(shí)現(xiàn)登錄驗(yàn)證與跳轉(zhuǎn)邏輯
在LoginPage.xaml.cs中,添加登錄按鈕點(diǎn)擊事件,驗(yàn)證賬號(hào)密碼后跳轉(zhuǎn)到首頁(yè)(HomePage):
using System.Windows;
using System.Windows.Controls;
?
namespace WpfLoginDemo
{
public partial class LoginPage : Page
{
public LoginPage()
{
InitializeComponent();
}
?
private void BtnLogin_Click(object sender, RoutedEventArgs e)
{
// 1. 獲取輸入的賬號(hào)和密碼(PasswordBox需用Password屬性,而非Text)
string account = TxtAccount.Text.Trim();
string pwd = TxtPwd.Password.Trim();
?
// 2. 簡(jiǎn)單登錄驗(yàn)證(實(shí)際項(xiàng)目需對(duì)接數(shù)據(jù)庫(kù)/接口,此處用固定值演示)
if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(pwd))
{
MessageBox.Show("賬號(hào)或密碼不能為空!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (account == "admin" && pwd == "123456") // 模擬正確賬號(hào)密碼
{
// 3. 驗(yàn)證通過,跳轉(zhuǎn)到首頁(yè)(HomePage需提前創(chuàng)建)
this.NavigationService.Navigate(new HomePage());
}
else
{
MessageBox.Show("賬號(hào)或密碼錯(cuò)誤!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
// 清空密碼框
TxtPwd.Password = "";
}
}
}
}步驟 4:創(chuàng)建首頁(yè)(HomePage.xaml)
登錄后跳轉(zhuǎn)的目標(biāo)頁(yè)面,示例如下:
<Page x:Class="WpfLoginDemo.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="首頁(yè)">
<Grid>
<TextBlock Text="登錄成功!歡迎進(jìn)入首頁(yè)" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<!-- 可添加退出登錄按鈕,跳回登錄頁(yè):this.NavigationService.Navigate(new LoginPage()); -->
</Grid>
</Page>三、方案 2:Window+UserControl 切換(單窗口場(chǎng)景)
適合登錄頁(yè)與首頁(yè)集成在同一個(gè) Window 中,通過切換 UserControl 實(shí)現(xiàn) “跳轉(zhuǎn)”,界面更緊湊(無獨(dú)立窗口切換感)。
步驟 1:創(chuàng)建登錄和首頁(yè)的 UserControl
- 登錄 UserControl:
LoginUC.xaml(XAML 結(jié)構(gòu)同方案 1 的 LoginPage,僅根標(biāo)簽改為<UserControl>) - 首頁(yè) UserControl:
HomeUC.xaml(XAML 結(jié)構(gòu)同方案 1 的 HomePage,僅根標(biāo)簽改為<UserControl>)
步驟 2:創(chuàng)建主 Window(承載 UserControl)
新建MainWindow.xaml,用一個(gè) Grid(ContentContainer)作為 UserControl 的容器,默認(rèn)加載登錄 UC:
<Window x:Class="WpfLoginDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfLoginDemo" <!-- 引用本地命名空間 -->
Title="WPF登錄示例" Width="800" Height="600" WindowStartupLocation="CenterScreen">
<Grid>
<!-- UserControl容器:默認(rèn)加載登錄UC -->
<Grid x:Name="ContentContainer">
<local:LoginUC/>
</Grid>
</Grid>
</Window>步驟 3:通過事件傳遞實(shí)現(xiàn)跳轉(zhuǎn)(關(guān)鍵)
由于 UserControl 無法直接操作 Window 的容器,需通過事件委托將登錄成功的信號(hào)傳遞給 MainWindow,再由 MainWindow 切換 UC:
在 LoginUC.xaml.cs 中定義登錄成功事件:
using System;
using System.Windows;
using System.Windows.Controls;
?
namespace WpfLoginDemo
{
public partial class LoginUC : UserControl
{
// 定義登錄成功事件(供MainWindow訂閱)
public event Action OnLoginSuccess;
?
public LoginUC()
{
InitializeComponent();
}
?
private void BtnLogin_Click(object sender, RoutedEventArgs e)
{
// 1. 登錄驗(yàn)證邏輯(同方案1)
string account = TxtAccount.Text.Trim();
string pwd = TxtPwd.Password.Trim();
?
if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(pwd))
{
MessageBox.Show("賬號(hào)或密碼不能為空!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (account == "admin" && pwd == "123456")
{
// 2. 驗(yàn)證通過,觸發(fā)登錄成功事件
OnLoginSuccess?.Invoke();
}
else
{
MessageBox.Show("賬號(hào)或密碼錯(cuò)誤!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
TxtPwd.Password = "";
}
}
}
}在 MainWindow.xaml.cs 中訂閱事件并切換 UC:
using System.Windows;
?
namespace WpfLoginDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
?
// 1. 獲取當(dāng)前加載的LoginUC
if (ContentContainer.Children[0] is LoginUC loginUC)
{
// 2. 訂閱登錄成功事件
loginUC.OnLoginSuccess += () =>
{
// 3. 事件觸發(fā):清空容器,加載首頁(yè)UC
ContentContainer.Children.Clear();
ContentContainer.Children.Add(new HomeUC());
};
}
}
}
}四、兩種方案對(duì)比與優(yōu)化建議
| 方案 | 優(yōu)點(diǎn) | 缺點(diǎn) | 適用場(chǎng)景 |
|---|---|---|---|
| NavigationWindow+Page | 實(shí)現(xiàn)簡(jiǎn)單,無需事件傳遞 | 窗口切換有 “閃動(dòng)感”,導(dǎo)航欄需隱藏 | 多獨(dú)立頁(yè)面(如帶注冊(cè)頁(yè)、忘記密碼頁(yè)) |
| Window+UserControl | 單窗口集成,界面更流暢 | 需通過事件傳遞信號(hào),略復(fù)雜 | 緊湊的單窗口應(yīng)用(如管理系統(tǒng)) |
優(yōu)化建議:
- 密碼安全:實(shí)際項(xiàng)目中,密碼不能明文驗(yàn)證,需用 MD5/SHA256 加密后與數(shù)據(jù)庫(kù)存儲(chǔ)的加密值對(duì)比。
- 登錄狀態(tài)保存:登錄成功后,可通過
Application.Current.Properties存儲(chǔ)用戶信息(如Application.Current.Properties["UserName"] = account;),供后續(xù)頁(yè)面使用。 - 退出登錄:首頁(yè)可添加 “退出登錄” 按鈕,點(diǎn)擊后跳回登錄頁(yè)(方案 1 用
NavigationService.Navigate(new LoginPage()),方案 2 切換回LoginUC)。
以上就是在C# WPF中實(shí)現(xiàn)登錄頁(yè)面跳轉(zhuǎn)的兩種方案的詳細(xì)內(nèi)容,更多關(guān)于C# WPF登錄頁(yè)面跳轉(zhuǎn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#表達(dá)式樹Expression動(dòng)態(tài)創(chuàng)建表達(dá)式
這篇文章介紹了C#表達(dá)式樹Expression動(dòng)態(tài)創(chuàng)建表達(dá)式的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
C#中使用Lambda表達(dá)式自定義比較器實(shí)現(xiàn)兩個(gè)列表合并實(shí)例
這篇文章主要介紹了C#中使用Lambda表達(dá)式自定義比較器實(shí)現(xiàn)兩個(gè)列表的合并實(shí)例,本文給出示例代碼和運(yùn)行效果,需要的朋友可以參考下2014-10-10
Winform界面中實(shí)現(xiàn)通用工具欄按鈕的事件處理方法
下面小編就為大家分享一篇Winform界面中實(shí)現(xiàn)通用工具欄按鈕的事件處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11
C#實(shí)現(xiàn)多文件打包壓縮(.Net?Core)
本文詳細(xì)講解了.Net?Core框架下C#實(shí)現(xiàn)多文件打包壓縮的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實(shí)現(xiàn)
本文主要介紹了C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

