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 ...
随机推荐
- 基于 CentOS 7 搭建 SVN
⒈安装 SVN 服务端 1.安装 Subversion Subversion 是一个版本控制系统,相对于的 RCS . CVS ,采用了分支管理系统,它的设计目标就是取代 CVS . yum inst ...
- tp5支付宝和微信支付
一.生成二维码给用户进行扫码支付 1.先在vendor目录下加入支付宝和微信支付的引用 2.付款处调用 /** * 订单支付接口 * * @api {post} {:url('order/pay')} ...
- HTML 标签的 for 属性
HTML 标签的 for 属性 for 属性规定 label 与哪个表单元素绑定. 隐式和显式的联系 label通常以下面两种方式中的一种来和表单控件相联系: 将表单控件作为标记标签的内容,这样的就是 ...
- python病毒
介绍 今天碰到一个有趣的python病毒,在这里https://github.com/cranklin/Python-Virus/blob/master/pythonvirus.py#L37 源码 分 ...
- MySQL中的DML、DQL和子查询
一.MySQL中的DML语句 1.使用insert插入数据记录: INSERT INTO `myschool`.`student` (`studentNo`, `loginPwd`, `student ...
- 异常-try...catch的方式处理异常2
package cn.itcast_02; /* * A:一个异常 * B:二个异常的处理 * a:每一个写一个try...catch * b:写一个try,多个catch * try{ * ... ...
- python之项目依赖管理
生成所有依赖清单 requirements.txt 1. pipreqs 工具 安装) pip install pipreqs 执行生成依赖列表命令) pipreqs ./ 完善版本: pipreq ...
- vue-路由动态切换title
router.js { path: '/nav', component: () => import('../view/nav.vue'), meta:{ title:'nav', } }, { ...
- deep_learning_Function_ lambda函数详解
这里总结了关于 Python 中的 lambda 函数的“一个语法,三个特性,四个用法”. 一个语法: 在 Python 中,lambda 函数的语法是唯一的.其形式如下: lambda argume ...
- Centos7 更改网卡名称
cd /etc/sysconfig/network-scripts/ 将要改名的网卡配置文件重命名,例如 mv ifcfg-eth1 ifcfg-eth0 vim ifcfg-eth0 修改devic ...