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

C# WPF實現(xiàn)頁面跳轉(zhuǎn)的兩種方法介紹

 更新時間:2025年10月22日 08:49:40   作者:張謹?shù)W  
在 C# WPF 中,頁面跳轉(zhuǎn)通常有兩種主要方式:使用 NavigationWindow+Page或在 Window 中切換 UserControl,下面我們來看看具體實現(xiàn)方法吧

在 C# WPF 中,頁面跳轉(zhuǎn)通常有兩種主要方式:使用 NavigationWindow+Page或在 Window 中切換 UserControl。以下是具體實現(xiàn)方法:

一、使用 NavigationWindow+Page 實現(xiàn)跳轉(zhuǎn)(適合導(dǎo)航場景)

1. 創(chuàng)建導(dǎo)航窗口(NavigationWindow)

NavigationWindow 是專門用于頁面導(dǎo)航的窗口,自帶導(dǎo)航按鈕(前進 / 后退)。

步驟 1:新建 NavigationWindow

在項目中添加一個NavigationWindow(如MainNavWindow.xaml):

<!-- MainNavWindow.xaml -->
<NavigationWindow x:Class="WpfApp.MainNavWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  Title="導(dǎo)航窗口" Width="800" Height="600"
                  Source="Page1.xaml"> <!-- 設(shè)置初始頁面 -->
</NavigationWindow>

步驟 2:設(shè)置啟動窗口

App.xaml中指定啟動窗口為MainNavWindow

<Application x:Class="WpfApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainNavWindow.xaml"> <!-- 改為導(dǎo)航窗口 -->
</Application>

2. 創(chuàng)建 Page 頁面

新建多個Page(如Page1.xaml、Page2.xaml)作為跳轉(zhuǎn)目標:

Page1.xaml(包含跳轉(zhuǎn)到 Page2 的按鈕):

<Page x:Class="WpfApp.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Page1">
    <StackPanel>
        <TextBlock FontSize="20" Text="這是頁面1"/>
        <Button Content="跳轉(zhuǎn)到頁面2" Click="GoToPage2"/>
    </StackPanel>
</Page>

Page1.xaml.cs(實現(xiàn)跳轉(zhuǎn)邏輯):

private void GoToPage2(object sender, RoutedEventArgs e) {
    // 跳轉(zhuǎn)到Page2
    this.NavigationService.Navigate(new Page2());
}

3. 常用導(dǎo)航操作

返回上一頁:

if (this.NavigationService.CanGoBack) {
    this.NavigationService.GoBack(); // 后退
}

前進到下一頁:

if (this.NavigationService.CanGoForward) {
    this.NavigationService.GoForward(); // 前進
}

刷新當前頁:

this.NavigationService.Refresh();

二、在 Window 中切換 UserControl(適合單窗口應(yīng)用)

如果不需要導(dǎo)航歷史,可在Window中通過切換UserControl實現(xiàn) “頁面” 切換,更靈活。

1. 創(chuàng)建 UserControl(模擬頁面)

新建兩個UserControl(如UC_Home.xaml、UC_Settings.xaml):

UC_Home.xaml

<UserControl x:Class="WpfApp.UC_Home"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock FontSize="20" Text="這是首頁"/>
</UserControl>

UC_Settings.xaml

<UserControl x:Class="WpfApp.UC_Settings"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock FontSize="20" Text="這是設(shè)置頁"/>
</UserControl>

2. 在 Window 中切換 UserControl

在主窗口中用一個容器(如Grid)承載UserControl,通過代碼動態(tài)切換:

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="切換頁面示例" Width="800" Height="600">
    <Grid>
        <!-- 導(dǎo)航按鈕 -->
        <StackPanel Orientation="Horizontal" Margin="10">
            <Button Content="首頁" Click="ShowHome" Margin="5"/>
            <Button Content="設(shè)置" Click="ShowSettings" Margin="5"/>
        </StackPanel>
        
        <!-- 頁面容器(用于顯示UserControl) -->
        <Grid x:Name="ContentContainer" Margin="10,50,10,10"/>
    </Grid>
</Window>

MainWindow.xaml.cs(切換邏輯):

private void ShowHome(object sender, RoutedEventArgs e) {
    // 清空容器,添加首頁UserControl
    ContentContainer.Children.Clear();
    ContentContainer.Children.Add(new UC_Home());
}
?
private void ShowSettings(object sender, RoutedEventArgs e) {
    // 清空容器,添加設(shè)置頁UserControl
    ContentContainer.Children.Clear();
    ContentContainer.Children.Add(new UC_Settings());
}

三、兩種方式的對比

方式優(yōu)點缺點適用場景
NavigationWindow+Page自帶導(dǎo)航歷史(前進 / 后退)導(dǎo)航欄樣式固定,定制性差類似瀏覽器的多頁面導(dǎo)航
Window+UserControl完全自定義布局,靈活度高需手動實現(xiàn)導(dǎo)航歷史(如需要)單窗口應(yīng)用,如管理系統(tǒng)界面

根據(jù)需求選擇合適的方式,小型應(yīng)用推薦用Window+UserControl,需要完整導(dǎo)航功能時用NavigationWindow+Page。

到此這篇關(guān)于C# WPF實現(xiàn)頁面跳轉(zhuǎn)的兩種方法介紹的文章就介紹到這了,更多相關(guān)WPF頁面跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

木里| 遵化市| 富顺县| 云浮市| 洞口县| 宜兰市| 甘洛县| 普格县| 邵阳县| 南靖县| 桦南县| 赤城县| 敦煌市| 安泽县| 新竹县| 广南县| 佛山市| 临猗县| 商洛市| 油尖旺区| 濮阳县| 洛川县| 无棣县| 封开县| 黑河市| 邻水| 宁津县| 武平县| 五家渠市| 南郑县| 富锦市| 周口市| 买车| 综艺| 塔河县| 新田县| 甘肃省| 稷山县| 江源县| 南丰县| 株洲市|