Prism中的containerRegister1
实现功能
通过点击不同的Button,实现显示不同的界面
思路
- 新建项-WPF-删除APP,完成两个模块,两个模块中写上约定
- Prism WPF模板,完成Views中UI部分
- 在Views中用两种不同的注册方式完成功能实现
模块
ModuleA
新建WPF,命名ModuleA,删除APP
定义模块A
- 新建文件夹Views,新建类,命名ViewA
约定文件
新建类,命名ModuleAProfile
实现接口IModule
public class ModuleAProfile : IModule
写约,写上容器服务
{
//约定,容器服务
public class ModuleAProfile : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
}
}
}
ModuleB
同ModuleA
Views
MainWindow.xaml
打格子
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
写两个Button
<StackPanel Orientation="Horizontal">
<Button Margin="5"
Command="{Binding OpenCommand}"
CommandParameter="ViewA"
Content="打开模块A" />
<Button Margin="5"
Command="{Binding OpenCommand}"
CommandParameter="ViewB"
Content="打开模块B" />
</StackPanel>
模块内容 介绍ContentControl2
<ContentControl Grid.Row="1"
prism:RegionManager.RegionName="ContentRegion" />
MainWindow.xaml.cs
using System.Windows;
namespace WpfApp3.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
App.xaml
这里有两种方式注册模块,一种是通过引用,一种是写文件位置
引用
添加ConfigureModuleCatalog方法
添加名称空间
using ModuleA;
using ModuleB;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<ModuleAProfile>();
moduleCatalog.AddModule<ModuleBProfile>();
base.ConfigureModuleCatalog(moduleCatalog);
}
}
}
通过写文件位置的方式
-
去掉WpfApp3中的引用
-
将
ModuleA.dll
, ModuleB.dll
文件拷贝到WpfApp3.dll
所在文件夹下的Modules
文件夹,若没有,则新建
添加ConfigureModuleCatalog方法
using Prism.Ioc;
using Prism.Modularity;
using System.Windows;
using WpfApp3.Views;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog(){ModulePath =@".\Modules" };
}
}
}
ViewModels
MainWindowViewModel.cs
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
namespace WpfApp3.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public DelegateCommand<string> OpenCommand { get; private set; }
public MainWindowViewModel()
{
}
private readonly IRegionManager regionManager;
public MainWindowViewModel(IRegionManager regionManger)
{
OpenCommand = new DelegateCommand<string>(Open);
this.regionManager = regionManger;
}
private void Open(string obj)
{
//首先通过IregionManager接口获取全局定义的区域
//往这个区域动态的去设置内容
//设置内容的方式是依赖注入
regionManager.Regions["ContentRegion"].RequestNavigate(obj);
}
}
}
Prism中的containerRegister
根据网上的搜索结果[1](https://stackoverflow.com/questions/56741827/prism-7-1-icontainerprovider-icontainerregistry)[2](https://prismlibrary.com/docs/dependency-injection/appendix.html)[3](https://prismlibrary.com/docs/dependency-injection/registering-types.html),containerRegistry 是一个用于注册类型和实例的接口,可以在 Prism 应用程序中使用依赖注入来解析它们。containerRegistry 可以在 PrismApplication 类中的 RegisterTypes 方法中使用,也可以在模块类中的 RegisterTypes 方法中使用。
containerRegistry 提供了多种方法来注册类型和实例,例如:
- RegisterInstance:注册一个已经存在的对象实例。
- RegisterSingleton:注册一个类型作为单例,每次解析都返回同一个对象实例。
- Register:注册一个类型作为瞬态,每次解析都返回一个新的对象实例。
- RegisterMany:注册一个类型作为多个接口或基类的实现。
例如,如果你想注册 ShellViewModel 类作为单例,你可以在 containerRegistry 中写:
containerRegistry.RegisterSingleton();
希望这些信息能对你有所帮助,如果你还有其他问题,请随时提问。 ↩︎
介绍ContentControl
ContentControl控件是一种可以添加和自定义的控件,用于在模板、表单和文档中包含特定类型的内容[1](https://support.microsoft.com/en-us/office/about-content-controls-283b1e29-0b77-4781-b236-2d02c1cce1c2)[2](https://learn.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-content-controls)。例如,你可以使用ContentControl控件来创建一个下拉列表,让用户从有限的选项中选择[1](https://support.microsoft.com/en-us/office/about-content-controls-283b1e29-0b77-4781-b236-2d02c1cce1c2)。
ContentControl控件可以包含任意类型的内容,如日期、列表或格式化文本[2](https://learn.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-content-controls)。你可以通过设置ContentControl控件的属性来控制其外观和行为,如标题、标签、颜色、锁定等[1](https://support.microsoft.com/en-us/office/about-content-controls-283b1e29-0b77-4781-b236-2d02c1cce1c2)[2](https://learn.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-content-controls)。
在WPF中,ContentControl控件是一个基类,有很多派生类,如Button、Label、CheckBox等[3](https://blog.youkuaiyun.com/BYH371256/article/details/125513486)[4](https://zhuanlan.zhihu.com/p/358449148)。你可以使用XAML或代码来创建和操作ContentControl控件[3](https://blog.youkuaiyun.com/BYH371256/article/details/125513486)[4](https://zhuanlan.zhihu.com/p/358449148)[5](https://zhuanlan.zhihu.com/p/514311839)。
在Word中,ContentControl对象是一个代表文档中内容控件的对象,你可以使用VBA来插入、删除、修改或查询内容控件的属性和方法[6](https://learn.microsoft.com/zh-cn/office/vba/api/word.contentcontrol)。
↩︎