Prism框架的Regions使用
Prism框架的Regions,可以把用户控件、窗体等附加到主窗体指定的控件中。
【实战1】

1、新建Prism Blank App(WPF) 项目:Demo0810
Views文件夹处,鼠标右键——添加——新建项——Prism——Prism UserControl(WPF),名称默认
MainWindow.xaml.cs代码:
using System.Windows;
using Prism.Regions; //引入Regions namespace Demo0810.Views
{
public partial class MainWindow : Window
{
public MainWindow(IRegionManager regionManager) //定义变量
{
InitializeComponent();
//将PrismUserControl1用户控件加载到主窗体的ContenRegion控件中
regionManager.RegisterViewWithRegion("ContentRegion", typeof(PrismUserControl1));
////或者
//PrismUserControl1 viewA = new PrismUserControl1(); //new出一个类的对象
//_regionManager.AddToRegion("ContentRegion", viewA);
}
}
}
PrismUserControl1.xaml代码:其他文件原封不动
<UserControl x:Class="Demo0810.PrismUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<TextBlock Text="View A" FontSize="38" />
</Grid>
</UserControl>
或者MainWindow.xaml.cs不更改(保持极简纯粹),更改MainWindowViewModel.cs的代码:推荐
using Prism.Mvvm;
using Prism.Regions; //引入Regions namespace Demo0810.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public MainWindowViewModel(RegionManager regionManager) //定义变量
{
//将PrismUserControl1用户控件加载到主窗体的ContenRegion控件中
regionManager.RegisterViewWithRegion("ContentRegion", typeof(PrismUserControl1));
////或者
//PrismUserControl1 viewA = new PrismUserControl1(); //new出一个类的对象
//_regionManager.AddToRegion("ContentRegion", viewA);
}
}
}
【实战2】仿照实战1新建项目Demo08101、添加用户控件PrismUserControl1.cs

主窗体界面前端代码MainWindow.xaml:
<Window x:Class="Demo08101.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525">
<DockPanel LastChildFill="True">
<Button Command="{Binding ShowCommand}" DockPanel.Dock="Top" >Add View</Button>
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
</DockPanel>
</Window>
MainWindowViewModel.cs代码:其他文件原封不动
using System;
using Prism.Mvvm;
using Prism.Commands;
using Prism.Regions;
using Demo08101.Views; namespace Demo08101.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title //数据属性
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public DelegateCommand ShowCommand { get; set; } //命令属性 IRegionManager _regionManager; //定义变量
public void Show() //方法,展示用户控件到指定的主窗体控件中
{
_regionManager.RegisterViewWithRegion("ContentRegion", typeof(PrismUserControl1));
////或者
//PrismUserControl1 viewA = new PrismUserControl1(); //new出一个类的对象
//_regionManager.AddToRegion("ContentRegion", viewA);
} public MainWindowViewModel(IRegionManager regionManager)
{
_regionManager = regionManager; //赋值
this.ShowCommand = new DelegateCommand(new Action(Show)); //命令属性关联方法
}
}
}
Prism框架的Regions使用的更多相关文章
- Prism框架研究(一)
从今天起开始写一个Prism框架的学习博客,今天是第一篇,所以从最基本的一些概念开始学习这个基于MVVM的框架的学习,首先看一下Prism代表什么,这里引用一下比较官方的英文解释来看一下:Prism ...
- Prism框架的Module(模块化)编程
Prism框架用的是新版本的,Prism7.1.关于其中的变动,感兴趣的参考https://www.cnblogs.com/hicolin/p/8694892.html 如何告诉Shell(我们的宿主 ...
- Prism 框架解读之一系列
名词解释 1.什么是IOC IOC是 Inversion of Control的缩写,多数书籍翻译成"控制反转". IOC 和依赖注入(DI) 所谓依赖注入,就是由IOC容器在运行 ...
- WPF Step By Step 系列-Prism框架在项目中使用
WPF Step By Step 系列-Prism框架在项目中使用 回顾 上一篇,我们介绍了关于控件模板的用法,本节我们将继续说明WPF更加实用的内容,在大型的项目中如何使用Prism框架,并给予Pr ...
- WPF Prism框架下基于MVVM模式的命令、绑定、事件
Prism框架下的自定义路由事件和命令绑定 BaseCode XAML代码: <Button x:Class="IM.UI.CommandEx.PrismCommandEx" ...
- 在Prism 框架中,实现主程序与模块间 UI 的通信
背景: 在模块的UI中包含 TreeView 控件,在该树形控件的每一节点前面定义了一个复选框,如图 需求: 在两个不同的应用程序中使用该控件,而它在不同应用程序中的外观则并不一致,按照本例,即一个显 ...
- Prism框架中加载类库中时其中第三方类dll提示无法加载程序集
Prism框架是采用一种依赖注入的方式动态加载程序集,能够在程序需要加载的时候将程序集注入到里面去,实现程序的热插拔效果,而且采用这种框架能够让我们进行一个大项目的独立开发,在最近的一个项目中在独立开 ...
- 项目中使用Prism框架
Prism框架在项目中使用 回顾 上一篇,我们介绍了关于控件模板的用法,本节我们将继续说明WPF更加实用的内容,在大型的项目中如何使用Prism框架,并给予Prism框架来构建基础的应用框架,并且 ...
- Prism框架在项目中使用
本文大纲 1.Prism框架下载和说明 2.Prism项目预览及简单介绍. 3.Prism框架如何在项目中使用. Prism框架下载和说明 Prism框架是针对WPF和Silverlight的MVVM ...
随机推荐
- C++学习 之 程序的组成部分(部分知识笔记)
1.预处理器编译指令#include: 预处理器是在程序编译前运行的工具.预处理器编译指令是向预处理器发送的命令,总是以#为标识,include便是其中常见的一种,用于引用文件,比如:iostream ...
- 二项式定理+前缀Sigma
https://hihocoder.com/problemset/problem/1430 思路: 要用前缀去推Sigma总公式,比较方便.https://blog.csdn.net/weixin_3 ...
- css多种方式实现等宽布局
本文讲的等宽布局是在不手动设置元素宽度的情况下,使用纯css实现各个元素宽度都相当的效果. 1.使用table-cell实现(兼容ie8) <style> body,div{ margin ...
- sql server安装图解
1.进入安装中心:可以参考硬件和软件要求.可以看到一些说明文档 2.选择全新安装模式继续安装 3.输入产品秘钥:这里使用演示秘钥进行 4.在协议中,点击同意,并点击下一步按钮,继续安装 5.进入全局规 ...
- Jmeter之逻辑控制器/定时器
Jmeter逻辑控制器 更新中 线程组->添加->逻辑控制器->XX控制器 1.仅一次控制器 使用场景:线程数为1,登录1次,循环浏览N次. 如果,登录账号参数化,线程数为M时,登录 ...
- Lua 打印 table (支持双向引用的table)
网上搜了一下,挺多打印table的方案,基本思路都是一层一层递归遍历table.(我就是参考这种思路做的^_^) 但大部分都不支持双向引用的打印.我所指的双向引用,就是a引用b, b又直接或间接引用a ...
- 使用python操作kafka
使用python操作kafka目前比较常用的库是kafka-python库 安装kafka-python pip3 install kafka-python 生产者 producer_test.py ...
- 深入理解计算机系统 第十章 系统级I/O 第二遍
了解 Unix I/O 的好处 了解 Unix I/O 将帮助我们理解其他的系统概念 I/O 是系统操作不可或缺的一部分,因此,我们经常遇到 I/O 和其他系统概念之间的循环依赖.例如,I/O 在进程 ...
- 4.Shell内部命令
4.Shell内部命令内部命令是由shell自身提供的.如果某个内部命令的名称是一个简单命令的第一个单词,shell会直接执行这个命令,而不会启动其它程序.对于一些不可能或者不方便通过外部程序实现的功 ...
- QT 安卓 悬浮窗权限动态申请
一:申请方式: String ACTION_MANAGE_OVERLAY_PERMISSION = "android.settings.action.MANAGE_OVERLAY_PERMI ...