網(wǎng)站首頁 編程語言 正文
前言
本文主要講述如何在同一個窗體內(nèi),實現(xiàn)不同功能模塊的頁面切換。
一、準備工作
1.搭建一個簡單的mvvm項目結(jié)構(gòu)
首先搭建一個簡單的項目框架,然后有紅和綠兩個頁面,ViewModels中的Base 中簡單實現(xiàn)了ICommand 和 INotifyPropertyChanged接口
二、實現(xiàn)
1.使用Frame控件的方式實現(xiàn)
利用Frame的Source 屬性加載內(nèi)部的控件,使用Frame的時候,用于切換的頁面可以是UserControl 或者Page,如案例中使用的就是Page
實現(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.使用反射的方式實現(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.實現(xiàn)效果
總結(jié)
原文鏈接:https://blog.csdn.net/qq_39847278/article/details/128386432
相關(guān)推薦
- 2023-07-09 echart 設(shè)置柱狀圖y軸最大刻度
- 2023-02-12 一文帶你了解Golang中reflect反射的常見錯誤_Golang
- 2022-06-19 mybatis-plus的sql語句打印問題小結(jié)_MsSql
- 2022-07-07 C語言用Easyx繪制圍棋和象棋的棋盤_C 語言
- 2023-07-31 TypeError:cannot read property ‘getAttribute‘ of
- 2022-05-24 C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn)_C#教程
- 2022-07-10 Linux安裝及管理程序
- 2022-07-31 C++超詳細分析講解內(nèi)聯(lián)函數(shù)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支