WPF實(shí)現(xiàn)頁(yè)面的切換的示例代碼
前言
本文主要講述如何在同一個(gè)窗體內(nèi),實(shí)現(xiàn)不同功能模塊的頁(yè)面切換。
一、準(zhǔn)備工作
1.搭建一個(gè)簡(jiǎn)單的mvvm項(xiàng)目結(jié)構(gòu)

首先搭建一個(gè)簡(jiǎn)單的項(xiàng)目框架,然后有紅和綠兩個(gè)頁(yè)面,ViewModels中的Base 中簡(jiǎn)單實(shí)現(xiàn)了ICommand 和 INotifyPropertyChanged接口
二、實(shí)現(xiàn)
1.使用Frame控件的方式實(shí)現(xiàn)
利用Frame的Source 屬性加載內(nèi)部的控件,使用Frame的時(shí)候,用于切換的頁(yè)面可以是UserControl 或者Page,如案例中使用的就是Page
實(shí)現(xiàn)代碼如下:
<Window x:Class="WpfApp2.Views.MainView"
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:WpfApp2.Views"
xmlns:vm="clr-namespace:WpfApp2.ViewModels"
mc:Ignorable="d"
Title="MainView" Height="450" Width="800">
<Window.DataContext>
<vm:MainViewModel></vm:MainViewModel>
</Window.DataContext>
<DockPanel Grid.Column="0">
<StackPanel Background="LightBlue">
<RadioButton Command="{Binding ChangePageCommand}" CommandParameter="PageRedView.xaml" Content="紅色" Margin="10"></RadioButton>
<RadioButton Command="{Binding ChangePageCommand}" CommandParameter="PageGreenView.xaml" Content="綠色" Margin="10"></RadioButton>
</StackPanel>
<Frame NavigationUIVisibility="Hidden" Source="{Binding PageName}"/>
</DockPanel>
</Window>
注意:這里的CommandParameter傳入的是PageRedView.xaml文件
public class MainViewModel:ViewModelBase
{
private string _pageName;
public string PageName
{
get { return _pageName; }
set { _pageName = value; OnPropertyChanged(); }
}
public ICommand ChangePageCommand { get; set; }
public MainViewModel()
{
ChangePageCommand = new CommandBase(ChangePage);
}
private void ChangePage(object obj)
{
PageName = obj.ToString();
}
}
2.使用反射的方式實(shí)現(xiàn)
使用反射+ContentControl 的方式也可使用頁(yè)面切換,不過該方式下ContentControl 的Content不可以承接Page,Page只有Frame 和Window可以承接,但是可以承接UserControl。
首先將紅色和綠色兩個(gè)界面修改為UserControl并命名為UserControlRed和UserControlGreen ,然后修改代碼如下:
<DockPanel Grid.Column="0">
<StackPanel Background="LightBlue">
<RadioButton Command="{Binding ChangePageCommand}" CommandParameter="UserControlRed" Content="紅色" Margin="10"></RadioButton>
<RadioButton Command="{Binding ChangePageCommand}" CommandParameter="UserControlGreen" Content="綠色" Margin="10"></RadioButton>
</StackPanel>
<ContentControl Content="{Binding MainContent}"/>
</DockPanel>
public class MainViewModel:ViewModelBase
{
private FrameworkElement mainContent;
public FrameworkElement MainContent
{
get { return mainContent; }
set { mainContent = value; OnPropertyChanged(); }
}
public ICommand ChangePageCommand { get; set; }
public MainViewModel()
{
ChangePageCommand = new CommandBase(ChangePage);
}
private void ChangePage(object obj)
{
//【 * 】這里需要拼接路徑
Type type = Type.GetType("WpfApp2.Views." + obj.ToString());
MainContent = (FrameworkElement)System.Activator.CreateInstance(type);
}
}
3.實(shí)現(xiàn)效果

總結(jié)
到此這篇關(guān)于WPF實(shí)現(xiàn)頁(yè)面的切換的示例代碼的文章就介紹到這了,更多相關(guān)WPF 頁(yè)面切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C#連接并讀取MongoDB數(shù)據(jù)庫(kù)
這篇文章介紹了使用C#連接并讀取MongoDB數(shù)據(jù)庫(kù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C#實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行字符串腳本(優(yōu)化版)的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行字符串腳本(優(yōu)化版),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
C#實(shí)現(xiàn)獲取文本文件的編碼的一個(gè)類(區(qū)分GB2312和UTF8)
這篇文章主要介紹了C#實(shí)現(xiàn)獲取文本文件的編碼一個(gè)類,本文給出類可以自動(dòng)區(qū)分GB2312和UTF8,并同時(shí)給出了使用方法,需要的朋友可以參考下2014-09-09
C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題實(shí)例分析
這篇文章主要介紹了C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題,實(shí)例分析了C#動(dòng)態(tài)規(guī)劃算法的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

