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)); //命令属性关联方法 } } }