Prism框架的Regions,能夠把用戶控件、窗體等附加到主窗體指定的控件中。前端
【實戰1】框架
一、新建Prism Blank App(WPF) 項目:Demo0810this
Views文件夾處,鼠標右鍵——添加——新建項——Prism——Prism UserControl(WPF),名稱默認spa
MainWindow.xaml.cs代碼:code
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代碼:其餘文件原封不動xml
<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新建項目Demo0810一、添加用戶控件PrismUserControl1.csblog
主窗體界面前端代碼MainWindow.xaml:get
<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代碼:其餘文件原封不動string
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)); //命令屬性關聯方法 } } }