安装prism.DryIoc
修改app.xaml
<prism:PrismApplication x:Class="WpfApp3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp3"
xmlns:prism="http://prismlibrary.com/"
>
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
/// <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)
{
//throw new NotImplementedException();
}
}
安装 模板 prism Template pack
DirectoryModuleCatalog、XamlModuleCatalog、ConfigurationModuleCatalog 都间接继承IModuleCatalog
Prism 区域
》》 prism:ViewModelLocator.AutoWireViewModel=“True”,
》》View要和ViewModel自带匹配,不需要 this.DataContext = new ViewModel();
》》需要遵循一个些规定, ViewModels 中的View名称+ViewModel 这种命名规则
<Window x:Class="BlankApp2.Views.MainView"
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" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="模块A" Command="{Binding OpenCommand}" CommandParameter="ViewA"></Button>
<Button Content="模块B" Command="{Binding OpenCommand}" CommandParameter="ViewB"></Button>
</StackPanel>
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />
</Grid>
</Window>
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
namespace BlankApp2.ViewModels
{
public class MainViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
private readonly IRegionManager _regionManager;
public MainViewModel(IRegionManager regionManager)
{
this._regionManager = regionManager;
this.OpenCommand = new DelegateCommand<string>(Open);
}
private void Open(string obj)
{
_regionManager.Regions["ContentRegion"].RequestNavigate(obj);
}
public DelegateCommand<string> OpenCommand { get; private set; }
}
}
using BlankApp2.Views;
using Prism.Ioc;
using System.Windows;
namespace BlankApp2
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainView>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<ViewB>();
//可以不使用默认匹配规则,自己指定对应的上下文
//containerRegistry.RegisterForNavigation<ViewA,ViewAViewModel>();
//containerRegistry.RegisterForNavigation<ViewA,ViewBViewModel>();
}
}
}
模块化 《1》 强引用
》》》通过prism Blank APP WPF 创建一个wpf应用
》》》新建模块步骤
- 新建wpf应用程序
- nugut 》》Prism.DryIoc
- 删除 App.xaml AssembylyInfo.cs MainWindow.xaml 这三个文件,同时把项目修改 类库
- 新建Views 文件夹 存放 视图文件
- 新建一个 项目名称+Profile 命名的 类
using ModuleA.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA
{
public class ModuleAProfile : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
// throw new NotImplementedException();
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
//ViewA 是Views下面的 视图文件
containerRegistry.RegisterForNavigation<ViewA>();
}
}
}
需要在主程序 引用模块
模块化 《2》 利用目录,不需要引用这些模块的dll,上面的方式需要引用dll
using BlankApp2.Views;
using Prism.Ioc;
using Prism.Modularity;
using System.Windows;
namespace BlankApp2
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainView>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//containerRegistry.RegisterForNavigation<ViewA>();
//containerRegistry.RegisterForNavigation<ViewB>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
//moduleCatalog.AddModule<StudentProfile>();
//moduleCatalog.AddModule<ModuleAProfile>();
//moduleCatalog.AddModule<ModuleCProfile>();
//base.ConfigureModuleCatalog(moduleCatalog);
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
}
}
}
模块化 配置文件的方式可以使用App.config进行配置,也可以使用xml文件的方式。 都需要把模块dll引入
主程序 添加 配置文件app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf" />
</configSections>
<startup>
</startup>
<modules>
<module assemblyFile="ModuleA.dll" moduleType="ModuleA.ModuleAProfile, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleAProfile" startupLoaded="True" />
</modules>
</configuration>
xaml 配置文件
<m:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Prism.Modularity;assembly=Prism.Wpf">
<m:ModuleInfo ModuleName="ModuleAProfile"
ModuleType="ModuleA.ModuleAProfile, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</m:ModuleCatalog>
using ModuleA.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA
{
public class ModuleAProfile : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
// throw new NotImplementedException();
var regionManager = containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
//containerRegistry.RegisterForNavigation<ViewA>();
}
}
}