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);
    }
}
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘4 小时前
开发WPF项目时遇到的问题总结
wpf
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars1 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest1 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest2 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0072 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业5 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf
weixin_447103585 天前
WPF之绑定!
c#·wpf
DataIntel5 天前
wpf问题记录
wpf
蓝点lilac7 天前
C# WPF 内置解码器实现 GIF 动图控件
c#·.net·wpf·图像