wpf prism 《1》、区域 、模块化 StackPanel实现区域适配器
安装prism.DryIoc修改app.xamlprism:PrismApplicationx:ClassWpfApp3.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp3xmlns:prismhttp://prismlibrary.com/Application.Resources/Application.Resources/prism:PrismApplication/// summary/// Interaction logic for App.xaml/// /summarypublicpartialclassApp:PrismApplication{protectedoverrideWindowCreateShell(){returnContainer.ResolveMainWindow();}protectedoverridevoidRegisterTypes(IContainerRegistrycontainerRegistry){//throw new NotImplementedException();}}安装 模板 prism Template pack安装好 模板之后 选择模板创建项目上面手动修改的模板会自动修改好的DirectoryModuleCatalog、XamlModuleCatalog、ConfigurationModuleCatalog 都间接继承IModuleCatalogPrism 区域》》》 前台Windowx:ClassBlankApp1.Views.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title}Height350Width525GridContentControlprism:RegionManager.RegionNamect//Grid/Window# 定义Region的方法两种 一个是上面的 xaml界面指定另外一个代码指定 如下Windowx:ClassBlankApp1.Views.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title}Height350Width525GridContentControlx:Namect_region//Grid/WindowpublicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();# ct_region 就是xaml中的控件名称RegionManager.SetRegionName(ct_region,ct);}}》》》 ViewModeusingPrism.Mvvm;usingPrism.Regions;namespaceBlankApp1.ViewModels{publicclassMainWindowViewModel:BindableBase{privatestring_titlePrism Application;privateIRegionManager_regionManager;publicstringTitle{get{return_title;}set{SetProperty(ref_title,value);}}publicMainWindowViewModel(IRegionManagerregionManager){this._regionManagerregionManager;# UserControl1 就是用户控件 ct 是前台的内容区域名称regionManager.RegisterViewWithRegion(ct,typeof(UserControl1));}}}》》 prism:ViewModelLocator.AutoWireViewModel“True”》》View要和ViewModel自带匹配不需要 this.DataContext new ViewModel();》》需要遵循一个些规定 ViewModels 中的View名称ViewModel 这种命名规则Windowx:ClassBlankApp2.Views.MainViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title}Height350Width525GridGrid.RowDefinitionsRowDefinitionHeight50/RowDefinitionRowDefinition/RowDefinition/Grid.RowDefinitionsStackPanelOrientationHorizontalHorizontalAlignmentRightButtonContent模块ACommand{Binding OpenCommand}CommandParameterViewA/ButtonButtonContent模块BCommand{Binding OpenCommand}CommandParameterViewB/Button/StackPanelContentControl Grid.Row1prism:RegionManager.RegionNameContentRegion//Grid/WindowusingPrism.Commands;usingPrism.Mvvm;usingPrism.Regions;usingSystem;namespaceBlankApp2.ViewModels{publicclassMainViewModel:BindableBase{privatestring_titlePrism Application;publicstringTitle{get{return_title;}set{SetProperty(ref_title,value);}}privatereadonlyIRegionManager_regionManager;publicMainViewModel(IRegionManagerregionManager){this._regionManagerregionManager;this.OpenCommandnewDelegateCommandstring(Open);}privatevoidOpen(stringobj){_regionManager.Regions[ContentRegion].RequestNavigate(obj);}publicDelegateCommandstringOpenCommand{get;privateset;}}}usingBlankApp2.Views;usingPrism.Ioc;usingSystem.Windows;namespaceBlankApp2{/// summary/// Interaction logic for App.xaml/// /summarypublicpartialclassApp{protectedoverrideWindowCreateShell(){returnContainer.ResolveMainView();}protectedoverridevoidRegisterTypes(IContainerRegistrycontainerRegistry){containerRegistry.RegisterForNavigationViewA();containerRegistry.RegisterForNavigationViewB();//可以不使用默认匹配规则自己指定对应的上下文//containerRegistry.RegisterForNavigationViewA,ViewAViewModel();//containerRegistry.RegisterForNavigationViewA,ViewBViewModel();}}}StackPanel 实现自定义区域适配器 (Region Adapter)第一步创建自定义适配器类# 第一步创建自定义适配器类 StackPanelRegionAdapter 让它继承自 RegionAdapterBaseTusingPrism.Regions;usingSystem;usingSystem.Collections.Generic;usingSystem.Collections.Specialized;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;namespaceBlankApp1{internalclassStackPanelRegionAdapter:RegionAdapterBaseStackPanel{publicStackPanelRegionAdapter(IRegionBehaviorFactoryregionBehaviorFactory):base(regionBehaviorFactory){}// 核心方法将区域和StackPanel关联起来protectedoverridevoidAdapt(IRegionregion,StackPanelregionTarget){// 订阅区域中视图集合的变化事件region.Views.CollectionChanged(sender,e){// 当有视图被添加时将其添加到StackPanel中if(e.ActionNotifyCollectionChangedAction.Add){foreach(FrameworkElementelementine.NewItems){regionTarget.Children.Add(element);}}// 当有视图被移除时将其从StackPanel中移除elseif(e.ActionNotifyCollectionChangedAction.Remove){foreach(FrameworkElementelementine.OldItems){if(regionTarget.Children.Contains(element))regionTarget.Children.Remove(element);}}};}protectedoverrideIRegionCreateRegion(){//创建Region实例AllActiveRegion表示所有视图同时激活returnnewAllActiveRegion();}}}第二步注册自定义适配器usingSystem.Windows;usingSystem.Windows.Controls;usingBlankApp1.Views;usingPrism.Ioc;usingPrism.Regions;namespaceBlankApp1{/// summary/// Interaction logic for App.xaml/// /summarypublicpartialclassApp{protectedoverrideWindowCreateShell(){returnContainer.ResolveMainWindow();}protectedoverridevoidRegisterTypes(IContainerRegistrycontainerRegistry){}# 注册自定义适配器类protectedoverridevoidConfigureRegionAdapterMappings(RegionAdapterMappingsregionAdapterMappings){base.ConfigureRegionAdapterMappings(regionAdapterMappings);// 注册自定义适配器 会通过 Prism 的依赖注入容器来获取适配器实例regionAdapterMappings.RegisterMapping(typeof(StackPanel),Container.ResolveStackPanelAdapter());}}}第三步在 XAML 中使用Windowx:ClassBlankApp1.Views.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title}Height350Width525Grid!-- 现在StackPanel已经可以作为Prism区域使用了 --StackPanelprism:RegionManager.RegionNamect//Grid/Window上面 xaml 对应的 viewmodelusingPrism.Mvvm;usingPrism.Regions;namespaceBlankApp1.ViewModels{publicclassMainWindowViewModel:BindableBase{privatestring_titlePrism Application;privateIRegionManager_regionManager;publicstringTitle{get{return_title;}set{SetProperty(ref_title,value);}}publicMainWindowViewModel(IRegionManagerregionManager){this._regionManagerregionManager;# UserControl1 用户控件regionManager.RegisterViewWithRegion(ct,typeof(UserControl1));}}}模块化 《1》 强引用》》》通过prism Blank APP WPF 创建一个wpf应用》》》新建模块步骤新建wpf应用程序nugut 》》Prism.DryIoc删除 App.xaml AssembylyInfo.cs MainWindow.xaml 这三个文件同时把项目修改 类库新建Views 文件夹 存放 视图文件新建一个 项目名称Profile 命名的 类usingModuleA.Views;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceModuleA{publicclassModuleAProfile:IModule{publicvoidOnInitialized(IContainerProvidercontainerProvider){// throw new NotImplementedException();}publicvoidRegisterTypes(IContainerRegistrycontainerRegistry){//ViewA 是Views下面的 视图文件containerRegistry.RegisterForNavigationViewA();}}}需要在主程序 引用模块模块化 《2》 利用目录不需要引用这些模块的dll上面的方式需要引用dllusingBlankApp2.Views;usingPrism.Ioc;usingPrism.Modularity;usingSystem.Windows;namespaceBlankApp2{/// summary/// Interaction logic for App.xaml/// /summarypublicpartialclassApp{protectedoverrideWindowCreateShell(){returnContainer.ResolveMainView();}protectedoverridevoidRegisterTypes(IContainerRegistrycontainerRegistry){//containerRegistry.RegisterForNavigationViewA();//containerRegistry.RegisterForNavigationViewB();}protectedoverridevoidConfigureModuleCatalog(IModuleCatalogmoduleCatalog){//moduleCatalog.AddModuleStudentProfile();//moduleCatalog.AddModuleModuleAProfile();//moduleCatalog.AddModuleModuleCProfile();//base.ConfigureModuleCatalog(moduleCatalog);}protectedoverrideIModuleCatalogCreateModuleCatalog(){returnnewDirectoryModuleCatalog(){ModulePath.\Modules};}}}模块化 配置文件的方式可以使用App.config进行配置也可以使用xml文件的方式。 都需要把模块dll引入主程序 添加 配置文件app.config?xmlversion1.0encodingutf-8?configurationconfigSectionssectionnamemodulestypePrism.Modularity.ModulesConfigurationSection, Prism.Wpf//configSectionsstartup/startupmodulesmoduleassemblyFileModuleA.dllmoduleTypeModuleA.ModuleAProfile, ModuleA, Version1.0.0.0, Cultureneutral, PublicKeyTokennullmoduleNameModuleAProfilestartupLoadedTrue//modules/configurationxaml 配置文件m:ModuleCatalogxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mclr-namespace:Prism.Modularity;assemblyPrism.Wpfm:ModuleInfoModuleNameModuleAProfileModuleTypeModuleA.ModuleAProfile, ModuleA, Version1.0.0.0, Cultureneutral, PublicKeyTokennull//m:ModuleCatalogusingModuleA.Views;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceModuleA{publicclassModuleAProfile:IModule{publicvoidOnInitialized(IContainerProvidercontainerProvider){// throw new NotImplementedException();varregionManagercontainerProvider.ResolveIRegionManager();regionManager.RegisterViewWithRegion(ContentRegion,typeof(ViewA));}publicvoidRegisterTypes(IContainerRegistrycontainerRegistry){//containerRegistry.RegisterForNavigationViewA();}}}