WPF实现页面的切换的示例代码
作者:鲤籽鲲 时间:2023-09-26 21:35:27
前言
本文主要讲述如何在同一个窗体内,实现不同功能模块的页面切换。
一、准备工作
1.搭建一个简单的mvvm项目结构
首先搭建一个简单的项目框架,然后有红和绿两个页面,ViewModels中的Base 中简单实现了ICommand 和 INotifyPropertyChanged接口
二、实现
1.使用Frame控件的方式实现
利用Frame的Source 属性加载内部的控件,使用Frame的时候,用于切换的页面可以是UserControl 或者Page,如案例中使用的就是Page
实现代码如下:
<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.使用反射的方式实现
使用反射+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.实现效果
来源:https://blog.csdn.net/qq_39847278/article/details/128386432
标签:WPF,页面切换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android动态布局小结
2021-10-17 12:04:39
带你了解Java Maven的打包操作
2022-08-03 15:23:33
![](https://img.aspxhome.com/file/2023/7/68537_0s.jpg)
SpringBoot Bean被加载时进行控制
2022-10-01 14:06:53
![](https://img.aspxhome.com/file/2023/3/74773_0s.png)
MyBatis的注解使用、ORM层优化方式(懒加载和缓存)
2023-04-24 09:16:13
![](https://img.aspxhome.com/file/2023/8/86048_0s.jpg)
Java十分钟精通类 封装 继承
2023-11-25 10:55:58
Android编程获取网络连接方式及判断手机卡所属运营商的方法
2023-09-18 21:54:14
Spring框架学习之Cache抽象详解
2023-07-20 17:37:47
Java中BeanUtils.copyProperties基本用法与小坑
2021-11-04 22:09:01
![](https://img.aspxhome.com/file/2023/3/73053_0s.png)
Java线程的全方位详解
2023-04-11 14:02:55
![](https://img.aspxhome.com/file/2023/2/61042_0s.png)
JAVA使用commos-fileupload实现文件上传与下载实例解析
2022-07-08 03:47:13
Mybatis Interceptor 拦截器的实现
2022-11-02 05:05:44
5步学会使用VideoView播放视频
2023-09-12 05:51:07
Spring Boot 动态数据源示例(多数据源自动切换)
2021-07-03 04:27:02
SpringBoot配置外部静态资源映射问题
2021-07-22 00:27:50
![](https://img.aspxhome.com/file/2023/7/108357_0s.png)
C# 6.0 的知识梳理
2021-07-21 22:08:17
![](https://img.aspxhome.com/file/2023/0/113730_0s.png)
c#实现用SQL池,多线程定时批量执行SQL语句的方法
2023-12-25 01:03:55
Java并发编程之闭锁与栅栏的实现
2022-01-09 15:00:12
Android开发之android_gps定位服务简单实现
2023-07-31 20:02:25
![](https://img.aspxhome.com/file/2023/3/83553_0s.gif)
Java Pattern和Matcher字符匹配方式
2022-06-07 21:57:56
![](https://img.aspxhome.com/file/2023/5/93075_0s.png)
JAVA中Comparable接口和自定义比较器示例讲解
2023-11-20 22:16:32