網(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-04-03 PyTorch加載模型model.load_state_dict()問題及解決_python
- 2022-05-05 Flutter如何保證數(shù)據(jù)操作原子性詳解_Android
- 2022-10-20 利用Python的tkinter模塊實現(xiàn)界面化的批量修改文件名_python
- 2022-05-11 解決 IntelliJ IDEA 中 .propertise 文件保存后中文亂碼
- 2022-10-08 ASP.NET泛型一之泛型簡介與基本語法_實用技巧
- 2022-08-18 golang?select?機制和超時問題_Golang
- 2022-02-12 android button的圓角邊框及點擊效果實現(xiàn)
- 2022-05-21 Python實現(xiàn)歸一化算法詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習環(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】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支