当前位置: 首页 > article >正文

WPF的Prism框架的使用

安装Prism.DryIoc库:

Prism的区域和模块化:

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

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

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

<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>
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>();
    }
}
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文件:

namespace ModuleA
{
    public class ModuleAProfile : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
        }

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

主项目中的App.xaml:

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);
    }
}


http://www.kler.cn/a/551975.html

相关文章:

  • XML Schema anyAttribute 元素详解
  • cmake:定位Qt的ui文件
  • 【JavaEE进阶】MyBatis通过注解实现增删改查
  • 在 Visual Studio 中使用 C++ 利用 dump 文件查找问题原因和崩溃点
  • 【python】4_异常
  • [LeetCode力扣hot100]-链表
  • 扩散模型中的马尔可夫链设计演进:从DDPM到Stable Diffusion全解析
  • labelimg的xml文件转labelme的json文件
  • Android ListPreference使用
  • 图床 PicGo+GitHub+Typora的下载安装与使用
  • 基于 Spring Cloud + Sentinel 的全面流量治理方案
  • fatal: Out of memory, malloc failed (tried to allocate 524288000 bytes)
  • 《DeepSeek 一站式工作生活 AI 助手》
  • 提升接口性能之缓存
  • Spring Boot项目的基本设计步骤和相关要点介绍
  • Win10环境使用零讯ZeroNews内网穿透实现Deepseek对外服务
  • UDP
  • 全平台搭载旭日5!科沃斯GOAT智能割草机器人全新系列正式开售
  • 轻松搭建本地大语言模型(一)Ollama安装与使用
  • 山石网科×阿里云通义灵码,开启研发“AI智造”新时代