从PRISM开始学WPF(三)Prism-Region-更新至Prism7.1
原文:
从PRISM开始学WPF(三)Prism-Region-更新至Prism7.1
[7.1update]在开始前,我们先看下版本7.1中在本实例中的改动。
<prism:PrismApplication x:Class="Regions.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" xmlns:local="clr-namespace:Regions"> <Application.Resources> </Application.Resources> </prism:PrismApplication> 在6.x中,这里还是一个 using Prism.Ioc; using Prism.Unity; using Regions.Views; using System.Windows; namespace Regions { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { } } } 这里用了 0x3 Regionregions
简单的说,就是一个容器(区域适配器),用来装载Views的。这像WinForms中的Container控件Panel,里面可以放置其他控件。在PRISM中,Views也是用户控件(UserControl) Region的注册方式: ? 在 <Window x:Class="Regions.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/" Title="Shell" Height="350" Width="525"> <Grid> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> </Grid> </Window> 从这里开始,我们就不得不面对 XAML了。继续引用维基百科的介绍:
(⊙﹏⊙)。。。 好,这个很简单,不需要介绍了。然后我们看代码,首先在头部引入命名空间
注册视图指定了Region,接下来就为Region指定View了。 在Views目录下面,新建一个UserControl,并在Grid控件内,加一个TextBlock,写上字,标识这是View A。 依旧省去了大部分头部引用,代码都是在Prism-Samples-Wpf里的,这里贴出来仅方便大家无源码阅读 <UserControl x:Class="ViewDiscovery.Views.ViewA" ... d:DesignHeight="300" d:DesignWidth="300"> <Grid> <TextBlock Text="View A" FontSize="38" /> </Grid> </UserControl> 我们现在想要,在上一小节制定的Region内显示View A,怎么做呢?我们来看MainWindow的code-behind,即: using Prism.Regions; using System.Windows; namespace ViewDiscovery.Views { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow(IRegionManager regionManager) { InitializeComponent(); //view discovery regionManager.RegisterViewWithRegion("ContentRegion",typeof(ViewA)); } } } 看上去,他只用了一行代码就实现了,在Region中显示ViewA。可仔细看一下,这好像哪不太一样。最早我们的MainWindow.xaml.cs的构造函数是没有参数的,这里多了一个 依赖注入容器[7.1updated]虽然7.1不再使用Unity,但这不影响我们来了解依赖注入容器在prism中的作用 如果使用 Unity 实例化一个类,该类的构造函数依赖一个或多个其他类,则 Unity 会为构造函数自动创建参数中指定的被依赖的类的实例。上面MainWindow的构造函数中的regionManager参数,就是Unity自动创建的。 参考:IoC 依赖注入容器 Unity RegionManager代码中RegionManager的RegisterViewWithRegion方法将我们的视图(View)和区域适配器(Region)进行关联 RegionManager,它实现了IRegionManager接口。IRegionManager接口包含一个只读属性Regions,是Region的集合(这个集合是从xaml中获取的,也就是我们定义的那些),RegionManager的实例会使用他们,并将view注册给他们。 View Discovery和View Injection在Prism中有两种方式来定义视图与Region之间的映射关系——View Discovery和View Injection。 上面的 然后我们看一下View Injection: using Microsoft.Practices.Unity; using Prism.Regions; using System.Windows; namespace ViewInjection.Views { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { IUnityContainer _container; IRegionManager _regionManager; public MainWindow(IUnityContainer container,IRegionManager regionManager) { InitializeComponent(); _container = container; _regionManager = regionManager; } private void Button_Click(object sender,RoutedEventArgs e) { var view = _container.Resolve<ViewA>(); IRegion region = _regionManager.Regions["ContentRegion"]; region.Add(view); } } } 他从_regionManager中获取Region,然后使用IRegion.Add(View)的方式来向已有的Region中添加View。 视图的激活与取消激活private void Button_Click(object sender,RoutedEventArgs e) { //activate view a _region.Activate(_viewA); } private void Button_Click_1(object sender,RoutedEventArgs e) { //deactivate view a _region.Deactivate(_viewA); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |