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

首先搭建一個簡單的項(xiàng)目框架,然后有紅和綠兩個頁面,ViewModels中的Base 中簡單實(shí)現(xiàn)了ICommand 和 INotifyPropertyChanged接口
二、實(shí)現(xiàn)
1.使用Frame控件的方式實(shí)現(xiàn)
利用Frame的Source 屬性加載內(nèi)部的控件,使用Frame的時候,用于切換的頁面可以是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 的方式也可使用頁面切換,不過該方式下ContentControl 的Content不可以承接Page,Page只有Frame 和Window可以承接,但是可以承接UserControl。
首先將紅色和綠色兩個界面修改為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)頁面的切換的示例代碼的文章就介紹到這了,更多相關(guān)WPF 頁面切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)動態(tài)執(zhí)行字符串腳本(優(yōu)化版)的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)動態(tài)執(zhí)行字符串腳本(優(yōu)化版),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
C#實(shí)現(xiàn)獲取文本文件的編碼的一個類(區(qū)分GB2312和UTF8)
這篇文章主要介紹了C#實(shí)現(xiàn)獲取文本文件的編碼一個類,本文給出類可以自動區(qū)分GB2312和UTF8,并同時給出了使用方法,需要的朋友可以參考下2014-09-09
C#使用動態(tài)規(guī)劃解決0-1背包問題實(shí)例分析
這篇文章主要介紹了C#使用動態(tài)規(guī)劃解決0-1背包問題,實(shí)例分析了C#動態(tài)規(guī)劃算法的實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

