WPF的Prism框架的使用

安装Prism.DryIoc库:

Prism的区域和模块化:

一个区域可以显示一个用户控件

一个模块就是一个项目,也就是一个类库

动态切换用户控件的案例:

XML 复制代码
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <Button Margin="5" Content="打开模块A" Command="{Binding OpenCommand}" CommandParameter="ViewA"/>
        <Button Margin="5" Content="打开模块B" Command="{Binding OpenCommand}" CommandParameter="ViewB"/>
        <Button Margin="5" Content="打开模块C" Command="{Binding OpenCommand}" CommandParameter="ViewC"/>
    </StackPanel>
    <ContentControl prism:RegionManager.RegionName="ContentRegion" Grid.Row="1"/>
</Grid>
cs 复制代码
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<ViewC>();
    }
}
cs 复制代码
internal class MainViewModel : BindableBase
{
    private readonly IRegionManager regionManager;
    public DelegateCommand<string> OpenCommand { get; private set; }
    public MainViewModel(IRegionManager regionManager)
    {
        OpenCommand = new DelegateCommand<string>(Open);
        this.regionManager = regionManager;
    }

    private void Open(string obj)
    {
        regionManager.Regions["ContentRegion"].RequestNavigate(obj);
    }
}

多模块的情况下动态切换一个区域的用户控件:

大部分的代码是和上面一样的

模块A中的ModuleAProfile文件:

cs 复制代码
namespace ModuleA
{
    public class ModuleAProfile : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            // 依赖注入
            containerRegistry.RegisterForNavigation<ViewA>();
        }
    }
}

主项目中的App.xaml:

cs 复制代码
public partial class App
{
    protected override Window CreateShell()
    {
        return Container.Resolve<MainView>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
    }

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<ModuleAProfile>();
        moduleCatalog.AddModule<ModuleBProfile>();
        base.ConfigureModuleCatalog(moduleCatalog);
    }
}
相关推荐
绿龙术士15 小时前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng2 天前
【WPF】Opacity 属性的使用
wpf
姬激薄2 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)3 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心3 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo4 天前
9.1.领域驱动设计
wpf
大道随心4 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠4 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go4 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet4 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf