WPF Prism 7.0+ IModule(模块化)
源码下载:PrismCore.zip
安装程序包Prism.DryIoc
设置模块
regionManager.Regions["LoginRegion"].RequestNavigate(obj);
无参数导航
regionManager.RequestNavigate("LoginRegion", "LoginControl");
带参数导航
NavigationParameters pairs = new NavigationParameters();
pairs.Add("参数名", "参数值");
regionManager.Regions["LoginRegion"].RequestNavigate(obj, pairs);

创建模块:

LoginProfile.cs
public class LoginProfile : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<Views.LoginControl, ViewModels.LoginViewModel>();
}
}
LoginViewModel.cs
namespace LoginModule.ViewModels
{
public class LoginViewModel : BindableBase
{
public DelegateCommand LoginCommand { get; private set; }
public LoginViewModel()
{
LoginCommand = new DelegateCommand(OnLogin);
}
private void OnLogin()
{
string username = UserName;
string password = Password;
}
/// <summary>
/// 账号
/// </summary>
private string _username = string.Empty;
public string UserName
{
get { return _username; }
//数据同步方法:RaisePropertyChanged();
set { _username = value; RaisePropertyChanged(); }
}
/// <summary>
/// 密码
/// </summary>
private string _password = string.Empty;
public string Password
{
get { return _password; }
//数据同步方法:RaisePropertyChanged();
set { _password = value; RaisePropertyChanged(); }
}
}
}
LoginControl.xaml
<UserControl x:Class="LoginModule.Views.LoginControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LoginModule.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Height="100" Background="Aquamarine">
<StackPanel>
<DockPanel>
<Label Content="账号:" Width="120" HorizontalContentAlignment="Right"/>
<TextBox Text="{Binding UserName}"/>
</DockPanel>
<DockPanel>
<Label Content="密码:" Width="120" HorizontalContentAlignment="Right"/>
<TextBox Text="{Binding Password}"/>
</DockPanel>
<DockPanel>
<Label Content="" Width="120"/>
<Button Content="登录" Width="120" HorizontalAlignment="Left" Command="{Binding LoginCommand}"/>
</DockPanel>
</StackPanel>
</Grid>
</UserControl>
主项目注册模块
public partial class App : PrismApplication
{
// Window窗体, StartupUri可以启动一个窗体
// CreateShell()主要负责启动一个主页面
protected override Window CreateShell()
{
// 启动一个窗体MainWindow
return Container.Resolve<Views.MainWindow>();
}
//通过IOC容器注册IApplicationCommands为单例
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
//注册子窗体模块
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
//添加模块A
moduleCatalog.AddModule<LoginModule.Profiles.LoginProfile>();
base.ConfigureModuleCatalog(moduleCatalog);
}
//方式2 如果不用 引用库的方式 可以重写此方法 把dll 放在同一文件夹
/*protected override IModuleCatalog CreateModuleCatalog()
{
//此 模块注入方式 是把所有相关的dll放在统一文件夹 Modules下,会检索所有实现了IModule接口的类,注册到容器
return new DirectoryModuleCatalog { ModulePath = @".\Modules" };
}*/
}
主项目切换模块:
public class MainWindowViewModel : BindableBase
{
private readonly IRegionManager regionManager;
public DelegateCommand<string> OpenModuleViewCommand { get; private set; }
public MainWindowViewModel(IRegionManager _regionManager)
{
OpenModuleViewCommand = new DelegateCommand<string>(OpenModuleView);
regionManager = _regionManager;
}
private void OpenModuleView(string obj)
{
regionManager.Regions["LoginContentRegion"].RequestNavigate(obj);
}
}
主项目使用模块:
<ContentControl prism:RegionManager.RegionName="LoginContentRegion"/>

浙公网安备 33010602011771号