最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在C#?WPF中實(shí)現(xiàn)登錄頁面跳轉(zhuǎn)的兩種方案

 更新時(shí)間:2025年10月22日 10:13:46   作者:張謹(jǐn)?shù)W  
在?C#?WPF?中實(shí)現(xiàn)登錄頁面跳轉(zhuǎn),核心是?“驗(yàn)證登錄邏輯”?與?“頁面切換”?結(jié)合,常用兩種方案:NavigationWindow?導(dǎo)航跳轉(zhuǎn)(適合多頁面場景)和Window+UserControl?切換(適合單窗口集成場景),本文介紹了具體實(shí)現(xiàn)步驟,需要的朋友可以參考下

引言

在 C# WPF 中實(shí)現(xiàn)登錄頁面跳轉(zhuǎn),核心是 “驗(yàn)證登錄邏輯” 與 “頁面切換” 結(jié)合,常用兩種方案:NavigationWindow 導(dǎo)航跳轉(zhuǎn)(適合多頁面場景)和Window+UserControl 切換(適合單窗口集成場景)。以下是具體實(shí)現(xiàn)步驟:

一、基礎(chǔ)準(zhǔn)備:創(chuàng)建登錄頁面結(jié)構(gòu)

無論哪種方案,先創(chuàng)建登錄頁面(包含賬號、密碼輸入框和登錄按鈕),這里以通用 XAML 結(jié)構(gòu)為例:

登錄頁面核心 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="登錄頁面">
    <!-- 用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"/>
            
            <!-- 賬號輸入框 -->
            <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="20,0">
                <TextBlock Text="賬號:" 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)(多頁面場景)

適合需要獨(dú)立登錄頁、且登錄后跳轉(zhuǎn)到其他 Page(如首頁)的場景,自帶導(dǎo)航歷史(但登錄頁通常無需后退,可隱藏導(dǎo)航欄)。

步驟 1:設(shè)置啟動(dòng)窗口為 NavigationWindow

修改App.xaml,指定啟動(dòng)窗口為NavigationWindow,并默認(rèn)加載登錄頁:

<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)航欄(避免登錄后可返回登錄頁):

<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"  <!-- 初始加載登錄頁 -->
                  ShowsNavigationUI="False"> <!-- 隱藏導(dǎo)航欄(關(guān)鍵) -->
</NavigationWindow>

步驟 3:實(shí)現(xiàn)登錄驗(yàn)證與跳轉(zhuǎn)邏輯

LoginPage.xaml.cs中,添加登錄按鈕點(diǎn)擊事件,驗(yàn)證賬號密碼后跳轉(zhuǎn)到首頁(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. 獲取輸入的賬號和密碼(PasswordBox需用Password屬性,而非Text)
            string account = TxtAccount.Text.Trim();
            string pwd = TxtPwd.Password.Trim();
?
            // 2. 簡單登錄驗(yàn)證(實(shí)際項(xiàng)目需對接數(shù)據(jù)庫/接口,此處用固定值演示)
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("賬號或密碼不能為空!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (account == "admin" && pwd == "123456") // 模擬正確賬號密碼
            {
                // 3. 驗(yàn)證通過,跳轉(zhuǎn)到首頁(HomePage需提前創(chuàng)建)
                this.NavigationService.Navigate(new HomePage());
            }
            else
            {
                MessageBox.Show("賬號或密碼錯(cuò)誤!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                // 清空密碼框
                TxtPwd.Password = "";
            }
        }
    }
}

步驟 4:創(chuàng)建首頁(HomePage.xaml)

登錄后跳轉(zhuǎn)的目標(biāo)頁面,示例如下:

<Page x:Class="WpfLoginDemo.HomePage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="首頁">
    <Grid>
        <TextBlock Text="登錄成功!歡迎進(jìn)入首頁" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <!-- 可添加退出登錄按鈕,跳回登錄頁:this.NavigationService.Navigate(new LoginPage()); -->
    </Grid>
</Page>

三、方案 2:Window+UserControl 切換(單窗口場景)

適合登錄頁與首頁集成在同一個(gè) Window 中,通過切換 UserControl 實(shí)現(xiàn) “跳轉(zhuǎn)”,界面更緊湊(無獨(dú)立窗口切換感)。

步驟 1:創(chuàng)建登錄和首頁的 UserControl

  • 登錄 UserControl:LoginUC.xaml(XAML 結(jié)構(gòu)同方案 1 的 LoginPage,僅根標(biāo)簽改為<UserControl>
  • 首頁 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 的容器,需通過事件委托將登錄成功的信號傳遞給 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("賬號或密碼不能為空!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (account == "admin" && pwd == "123456")
            {
                // 2. 驗(yàn)證通過,觸發(fā)登錄成功事件
                OnLoginSuccess?.Invoke();
            }
            else
            {
                MessageBox.Show("賬號或密碼錯(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ā):清空容器,加載首頁UC
                    ContentContainer.Children.Clear();
                    ContentContainer.Children.Add(new HomeUC());
                };
            }
        }
    }
}

四、兩種方案對比與優(yōu)化建議

方案優(yōu)點(diǎn)缺點(diǎn)適用場景
NavigationWindow+Page實(shí)現(xiàn)簡單,無需事件傳遞窗口切換有 “閃動(dòng)感”,導(dǎo)航欄需隱藏多獨(dú)立頁面(如帶注冊頁、忘記密碼頁)
Window+UserControl單窗口集成,界面更流暢需通過事件傳遞信號,略復(fù)雜緊湊的單窗口應(yīng)用(如管理系統(tǒng))

優(yōu)化建議:

  • 密碼安全:實(shí)際項(xiàng)目中,密碼不能明文驗(yàn)證,需用 MD5/SHA256 加密后與數(shù)據(jù)庫存儲的加密值對比。
  • 登錄狀態(tài)保存:登錄成功后,可通過Application.Current.Properties存儲用戶信息(如Application.Current.Properties["UserName"] = account;),供后續(xù)頁面使用。
  • 退出登錄:首頁可添加 “退出登錄” 按鈕,點(diǎn)擊后跳回登錄頁(方案 1 用NavigationService.Navigate(new LoginPage()),方案 2 切換回LoginUC)。

以上就是在C# WPF中實(shí)現(xiàn)登錄頁面跳轉(zhuǎn)的兩種方案的詳細(xì)內(nèi)容,更多關(guān)于C# WPF登錄頁面跳轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

多伦县| 红原县| 蒙城县| 庄浪县| 河津市| 桂东县| 西乌珠穆沁旗| 东山县| 诏安县| 嘉兴市| 宝丰县| 讷河市| 彭泽县| 洞头县| 济宁市| 肥东县| 社旗县| 庆元县| 许昌县| 襄垣县| 盖州市| 加查县| 泸西县| 牙克石市| 长子县| 桑植县| 洛宁县| 日土县| 泗洪县| 浦县| 卫辉市| 诸暨市| 平泉县| 丁青县| 肃宁县| 阿荣旗| 宜都市| 日照市| 桓台县| 齐齐哈尔市| 灯塔市|