Prism基础-项目初始化

Prism基础-项目初始化

前言

MVVM 是指将传统的页面和数据绑定的数据操作方式分割开,将视图为view(xmal),数据操作为viewmodel(cs)

创建项目

image

在NuGet中安装Prism.DryIoc(netcore)

App

App启动项目继承PrismApplication,前后需保持一致

<prism:PrismApplication x:Class="Prism模块.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Prism模块"
             xmlns:prism ="http://prismlibrary.com/"
            >
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>

     public partial class App : PrismApplication
    {
         // 启动页面
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }

         // 注册导航
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<ViewC, ViewCViewModel>();
        }
/**
         // 静态注入模块
         protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
         {
            moduleCatalog.AddModule<ModuleAProfile>();
            moduleCatalog.AddModule<ModuleBProfile>();
           base.ConfigureModuleCatalog(moduleCatalog);
         }

        // 动态注入模块
        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new DirectoryModuleCatalog() { ModulePath = @".\ModuleDll" };
        }
        */
    }

MainView

在主窗口中添加按钮,点击按钮会在指定区域添加指定view

<Window x:Class="Prism模块.Views.MainView"
        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:Prism模块"
        xmlns:prism ="http://prismlibrary.com/"
        mc:Ignorable="d"
        
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="区域" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <Button x:Name="btn1" Content="模块A" Margin="5" Command="{Binding OpeanCommand}" CommandParameter="ViewA"/>
            <Button x:Name="btn2" Content="模块B"  Margin="5" Command="{Binding OpeanCommand}" CommandParameter="ViewB"/>
            <Button x:Name="btn3" Content="模块C"  Margin="5" Command="{Binding OpeanCommand}" CommandParameter="ViewC"/>
        </StackPanel>

        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion"/>
    </Grid>
</Window>

MainViewModel

添加命令,当命令被触发时启动指定方法

namespace Prism模块.ViewModels
{
    public class MainViewModel : BindableBase
    {
        public DelegateCommand<string> OpeanCommand { get; private set; }

        private IRegionManager regionManager;

        public MainViewModel(IRegionManager regionManager)
        {
            OpeanCommand = new DelegateCommand<string>(Opean);
            this.regionManager = regionManager;
        }


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

ViewA

添加用户控件

<UserControl x:Class="Prism模块.View.ViewA"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Prism模块.View"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      >

    <Grid>
        <TextBlock Text="模块A" FontSize="30" />
    </Grid>
</UserControl>

随后在App.xmal中注册导航

  protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
        // 第一个参数为view 
        //第二个为与之对应的viewmodel(可不填),
        //第三个为注册的名字(可不填) 默认为视图名
         //   containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>("AAA");
            containerRegistry.RegisterForNavigation<ViewA>();
            containerRegistry.RegisterForNavigation<ViewB>();
            containerRegistry.RegisterForNavigation<ViewC>();
        }

程序在启动时,先 通过RegisterTypes()将viewA注入容器中,然后通过CreateShell()加载主界面.

点击按钮时,先触发 OpeanCommand命令,参数通过opean()方法传入容器中查找与之对应的导航,然后将找到的通过名称注入到指定名称的区域中。

posted @ 2022-02-14 00:43  阿狸的萝卜  阅读(238)  评论(0编辑  收藏  举报