首页 > 其他分享 >WPF MVVM框架------ Prism中的区域导航(RegionManager)

WPF MVVM框架------ Prism中的区域导航(RegionManager)

时间:2023-02-16 22:46:51浏览次数:37  
标签:obj MainContent MVVM regionManager 视图 Prism RegionManager 导航 页面

本次简单记录一下Prism中区域导航功能的使用

第一步 准备导航页面

新建两个UserControl (RegionFirstView.xaml ,RegionSecondView.xaml),作为导航的目标页面

简单在两个页面上做好标记

RegionFirstView.xaml

<Grid>
        <Border Background="Green"/>
        <TextBlock Text="RegionFirstView" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBox VerticalAlignment="Bottom"/>
</Grid>

RegionSecondView.xaml

<Grid>
        <Border Background="Red"/>
        <TextBlock Text="RegionSecondView" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>

第二步 依赖注入

将这两个页面注入到容器中,打开App.xmal.cs

public partial class App : PrismApplication
{
  protected override Window CreateShell()
  {
    return Container.Resolve<RegionManageView>();
  }

  protected override void RegisterTypes(IContainerRegistry containerRegistry)
  {
    containerRegistry.RegisterForNavigation<RegionFirstView>();
    containerRegistry.RegisterForNavigation<RegionSecondView>();
  }
}

第三步 主窗体布局

 注意:ViewModelLocator 类具有附加属性, AutoWireViewModel该属性用于将视图模型与视图相关联。 在视图的 XAML 中,此附加属性设置为 true 以指示视图模型应自动连接到视图

用于显示导航内容的 <ContentControl Grid.Column="1" prism:RegionManager.RegionName="MainContent"/>,为其区域起名为"MainContent"

<Window x:Class="MvvmBase.PrismDemo.Views.RegionManageView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MvvmBase.PrismDemo.Views"
        mc:Ignorable="d"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="RegionManageView" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <StackPanel Width="150">
            <Button Height="25"  Content="OpenRegionFirstView" Command="{Binding OpenRegionCommand}" CommandParameter="RegionFirstView"/>
            <Button Height="25" Content="OpenRegionSecondView" Command="{Binding OpenRegionCommand}" CommandParameter="RegionSecondView"/>
            <Button Height="25" Content="CloseRegionFirstView" Command="{Binding CloseRegionCommand}" CommandParameter="RegionFirstView"/>
            <Button Height="25" Content="CloseRegionSecondView" Command="{Binding CloseRegionCommand}" CommandParameter="RegionSecondView"/>
        </StackPanel>
        <ContentControl Grid.Column="1" prism:RegionManager.RegionName="MainContent"/>
    </Grid>
</Window>

 第四步  RegionManageViewModel 类实现

通过构造函数,容器将区域管理类注入进来,从而获取到 IRegionManager,可用于管理导航类

4.1 实现打开和关闭命令

  1)点击打开或关闭时,由于前端会通过CommandParameter将需要操作的页面传递到方法中,也就是 方法DoCloseRegionCommand(string obj)和DoOpenRegionCommand(string obj)的参数obj

  2)当执行打开命令OpenRegionCommand时,将会为区域MainContent 添加导航页面 ;假如传入obj为RegionFirstView,区域管理器将会从容器中寻找名字为RegionFirstView页面,并导航到该页面

  3)当执行打开命令CloseRegionCommand时,

  var views = regionManager.Regions["MainContent"]; // 获取区域 MainContent的所有视图
  var view =  views.Views.FirstOrDefault(it => it.GetType().Name == obj);//找到需要关闭的页面、
  views.Remove(view);// 将该页面移除

执行时,

 

    public class RegionManageViewModel:BindableBase
    {
        private readonly IRegionManager regionManager;

        public DelegateCommand<string> OpenRegionCommand { get; private set; }
        public DelegateCommand<string> CloseRegionCommand { get; private set; }
        public RegionManageViewModel(IRegionManager regionManager)
        {
            OpenRegionCommand = new DelegateCommand<string>(DoOpenRegionCommand);
            CloseRegionCommand = new DelegateCommand<string>(DoCloseRegionCommand);
            this.regionManager = regionManager;
        }

        private void DoCloseRegionCommand(string obj)
        {
            var views = regionManager.Regions["MainContent"];
            var view =  views.Views.FirstOrDefault(it => it.GetType().Name == obj);
            if(view!=default)
            {
                views.Remove(view);
            }
        }

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

第五步 演示

5.1 启动

 

5.2 打开RegionFirstView和RegionSecondView

 

 

 

5.3 点击移除 打开的导航

 

 

标签:obj,MainContent,MVVM,regionManager,视图,Prism,RegionManager,导航,页面
From: https://www.cnblogs.com/just-like/p/17128496.html

相关文章

  • 6 mvvm设计模式
    Vue的createApp和mount方法讲解​​简介​​​​最常见的Vue初级代码​​​​createApp()和mount()方法讲解​​​​createApp()方法的基本参数讲解​​​​如何获......
  • MVC与MVVM
    MVC:是Model(模型)View(视图)Controller(控制器)的缩写,是服务端分层开发的概念,本质上是用一种将数据、界面显示、业务逻辑分离的方法组织代码的软件开发设计典范。(1)Model:数据层,......
  • Prism 源码解读3-Modules加载
    在软件开发过程中,总想组件式的开发方式,各个组件之间最好互不影响,独立测试。Prism的Modules很好的满足了这一点。这个架构图很好了讲解了Prism的Modules的概念Prism支持......
  • MAUI新生2.5-数据绑定和MVVM:MVVM的属性验证
    一、MVVM的属性验证案例Toolkit.Mvvm框架中的ObservableValidator类,提供了属性验证功能,可以使用我们熟悉的验证特性对属性的值进行验证,并将错误属性提取和反馈给UI层。以......
  • Prism 资源
    Github:https://github.com/PrismLibrary/PrismNuGet:https://www.nuget.org/packages/prism.wpf安装Prism项目模板:打开VS,菜单:扩展-->管理扩展,搜索Prism ......
  • WinForm DevExpress Mvvm 模式介绍
    原文目录WinFormsMVVM|WinFormsControls|DevExpressDocumentation翻译目录(40条消息)【DevExpressMVVM】中文翻译系列.文章目录_hbwhypw的博客-CSDN博客_mvvm的......
  • WPF的MVVM的数据驱动学习示例
    项目结构  界面数据绑定<Windowx:Class="WpfApp1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="h......
  • MVVM 与 MVC 的区别
    MVVM(Model-View-ViewModel)和MVC(Model-View-Controller)都是软件架构模式,用于将应用程序的不同部分分离开来,更好地组织代码。MVC是一种模型-视图-控制器模式,其中......
  • day55 -mvvm模型
    mvvm模型 <!--MVVM模型1.M:模型model:data中的数据2.V:视图:模板代码3.VM:视图模型:Vue实例发现:1.data中的所有属性,最后都出现在vm身上......
  • 界面控件DevExpress WinForm——属于WinForm组件的MVVM框架
    DevExpressWinForm拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForm能完美构建流畅、美观且易于使用的应用程序,无论是Office风......