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

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

 更新時(shí)間:2025年10月22日 10:13:46   作者:張謹(jǐn)?shù)W  
在?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)步驟,需要的朋友可以參考下

引言

在 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)文章

最新評(píng)論

行唐县| 大理市| 闻喜县| 团风县| 道孚县| 湾仔区| 刚察县| 锡林浩特市| 蒙城县| 乌鲁木齐市| 绥江县| 叶城县| 潼南县| 五河县| 宝坻区| 阿图什市| 临武县| 宁德市| 曲松县| 阿坝县| 东乡族自治县| 大足县| 砚山县| 武夷山市| 乌拉特中旗| 乐平市| 海门市| 广南县| 偃师市| 思南县| 遵化市| 盱眙县| 宾川县| 海宁市| 普宁市| 陆川县| 海阳市| 镇宁| 新沂市| 平邑县| 玉山县|