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使用的更多相关文章

  1. Prism框架研究(一)

    从今天起开始写一个Prism框架的学习博客,今天是第一篇,所以从最基本的一些概念开始学习这个基于MVVM的框架的学习,首先看一下Prism代表什么,这里引用一下比较官方的英文解释来看一下:Prism ...

  2. Prism框架的Module(模块化)编程

    Prism框架用的是新版本的,Prism7.1.关于其中的变动,感兴趣的参考https://www.cnblogs.com/hicolin/p/8694892.html 如何告诉Shell(我们的宿主 ...

  3. Prism 框架解读之一系列

    名词解释 1.什么是IOC IOC是 Inversion of Control的缩写,多数书籍翻译成"控制反转". IOC 和依赖注入(DI) 所谓依赖注入,就是由IOC容器在运行 ...

  4. WPF Step By Step 系列-Prism框架在项目中使用

    WPF Step By Step 系列-Prism框架在项目中使用 回顾 上一篇,我们介绍了关于控件模板的用法,本节我们将继续说明WPF更加实用的内容,在大型的项目中如何使用Prism框架,并给予Pr ...

  5. WPF Prism框架下基于MVVM模式的命令、绑定、事件

    Prism框架下的自定义路由事件和命令绑定 BaseCode XAML代码: <Button x:Class="IM.UI.CommandEx.PrismCommandEx" ...

  6. 在Prism 框架中,实现主程序与模块间 UI 的通信

    背景: 在模块的UI中包含 TreeView 控件,在该树形控件的每一节点前面定义了一个复选框,如图 需求: 在两个不同的应用程序中使用该控件,而它在不同应用程序中的外观则并不一致,按照本例,即一个显 ...

  7. Prism框架中加载类库中时其中第三方类dll提示无法加载程序集

    Prism框架是采用一种依赖注入的方式动态加载程序集,能够在程序需要加载的时候将程序集注入到里面去,实现程序的热插拔效果,而且采用这种框架能够让我们进行一个大项目的独立开发,在最近的一个项目中在独立开 ...

  8. 项目中使用Prism框架

    Prism框架在项目中使用   回顾 上一篇,我们介绍了关于控件模板的用法,本节我们将继续说明WPF更加实用的内容,在大型的项目中如何使用Prism框架,并给予Prism框架来构建基础的应用框架,并且 ...

  9. Prism框架在项目中使用

    本文大纲 1.Prism框架下载和说明 2.Prism项目预览及简单介绍. 3.Prism框架如何在项目中使用. Prism框架下载和说明 Prism框架是针对WPF和Silverlight的MVVM ...

随机推荐

  1. 第二周Java课堂作业

    演示一: public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size ...

  2. Python 最常见的 170 道面试题解析:2019 最新

    Python 最常见的 170 道面试题解析:2019 最新 2019年06月03日 23:30:10 GitChat的博客 阅读数 21329 文章标签: PythonPython入门Python面 ...

  3. centos7 追加python3 + 使用pip + virtualenv

    一.安装Python3的方法: 首先安装依赖包: yum -y groupinstall "Development tools" yum -y install zlib-devel ...

  4. UI语言杂集

    最适合做 GUI 的是 DSL 或者 XML(以及 XML 的扩展)之类的标记语言,而不是编程语言. 例如 Qt 的 QML,Android 的 XML 或者 WPF 的 XAML 以及大家都再熟悉不 ...

  5. 怎样使用 v-html 指令?

    v-html 可以在目标节点位置内部插入 html 子节点, 跟节点的 .innerHTML 属性类似, 使用方法如下: <!DOCTYPE html> <html lang=&qu ...

  6. 进阶Java编程(2)线程常用操作方法

    线程常用操作方法 多线程的主要操作方法都在Thread类中定义的. 1,线程的命名和取得 多线程的运行状态是不确定的,那么在程序的开发之中为了可以获取到一些需要使用到的线程就只能依靠线程的名字来进行操 ...

  7. 解决VS2005打开js,css,asp.php等文件,中文都是乱码的问题

    用记事本打开可以正常观看但是用VS2005编辑器打开JS,中文确实乱码. 解决办法:在VS 2005 的设置里面选择自动检测Utf-8:“工具”->“选项”->“文本编辑器”->“自 ...

  8. c# winfrom 界面设计

    1.在用DotnetBar的RibbonControl时,界面最大化时,会把电脑桌面的任务栏遮盖住: 解决办法:在load事件中写入: , Screen.PrimaryScreen.WorkingAr ...

  9. C# WPF 数据绑定

    后台通知: public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(str ...

  10. 手把手教你搭建FastDFS集群(中)

    手把手教你搭建FastDFS集群(中) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u0 ...