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)); //命令属性关联方法 } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2018-08-10 下拉列表JComboBox,列表框JList
2018-08-10 按钮JButton,单选按钮JRadioButton,复选框JCheckBox