Prism框架学习

目录

一、使用prism框架

二、prism区域介绍

一、使用prism框架

1.建立一个项目,使用.net5.0框架

 2.在nuget中输入prism,然后选择prism.DryIoc安装

3. 打开App.xaml.cs文件,继承PrismApplication

using Prism.DryIoc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WPFPrism
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
    }
}

4.打开App.xaml文件,进行引用prism和修改

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

    </Application.Resources>
</prism:PrismApplication>

这里把 StartupUri="MainWindow.xaml"删除,否则会启动2个窗口

5.重新编译后,点击实现抽象类

using Prism.DryIoc;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WPFPrism
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
    protected override Window CreateShell()
    {
    //通过容器去拿Window
    return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
    //依赖注入的功能,增加依赖注入要实现的内容
    }
    }
}

注意:建立项目的时候不要包含.Prism,一开始我就进坑了 

6. 界面随便拖动一个控件,点击运行,效果

拓展:

使用模板建立速度更加的快。

这里是vs2022

1.在顶部拓展中搜素prism 

下载第一个 

2.进行安装

3.安装完成,需要重启vs2022,可以选择建立一个空项目

选择容器

4.项目文件预览

使用模板这里已经把需要修改的都修改完了。 

二、prism区域介绍

 1.首先进行布局

业务,点击按钮,弹框对应的区域窗体

 ​​​​​​​

<Window x:Class="WPFPrism.MainWindow"
        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:WPFPrism"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!--<Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" >
            <Button Content="打开模块A" Margin="5"/>
            <Button Content="打开模块B" Margin="5"/>
            <Button Content="打开模块C" Margin="5"/>
        </StackPanel>
        <ContentControl Grid.Row="1" />-->
    </Grid>
</Window>

2.建立3个窗体

<UserControl x:Class="WPFPrism.Views.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:WPFPrism.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Background="Red">
    <Grid>
        <TextBlock Text="我是A" FontSize="50"/>
    </Grid>
</UserControl>

A,B,C窗体代码以此类推 

3. 建立MainViewModle类​​​​​​​

using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFPrism.Views;

namespace WPFPrism.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager regionManager;

        public DelegateCommand<string> OpenCommand { get; set; }//创建命令,string用来接收参数,例如ViewA
        public MainViewModel(IRegionManager regionManager)//这里点击快速操作。拿界面定义的区域Content
        {
            OpenCommand = new DelegateCommand<string>(Open);
            this.regionManager = regionManager;//拿到这个值
        }


        private void Open(string obj)
        {
            regionManager.Regions["Content"].RequestNavigate(obj);//这里需要依赖注入的方式
        }
    }

}

4.在主界面增加RegionManager

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

5.修改MainViewModle类

using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFPrism.Views;

namespace WPFPrism.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager regionManager;

        public DelegateCommand<string> OpenCommand { get; set; }//创建命令,string用来接收参数,例如ViewA
        public MainViewModel(IRegionManager regionManager)//这里点击快速操作。拿界面定义的区域Content
        {
            OpenCommand = new DelegateCommand<string>(Open);
            this.regionManager = regionManager;//拿到这个值
        }


        private void Open(string obj)
        {
            regionManager.Regions["Content"].RequestNavigate(obj);//这里需要依赖注入的方式
        }
    }

}

注意:

文件夹下面的的层级,不能少了

例如:

<Window x:Class="WPFPrism.Views.MainView">

不能漏写Views文件夹的名字

因为MVVM修改代码的时候,不是顺序常规性的,总是前面改了,再改后面,所以导致前面的代码和成型的代码差异很大。

总体代码结构:

 MainViewModel.cs​​​​​​​

using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFPrism.Views;

namespace WPFPrism.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager regionManager;

        public DelegateCommand<string> OpenCommand { get; set; }//创建命令,string用来接收参数,例如ViewA
        public MainViewModel(IRegionManager regionManager)//这里点击快速操作。拿界面定义的区域Content
        {
            OpenCommand = new DelegateCommand<string>(Open);
            this.regionManager = regionManager;//拿到这个值
        }


        private void Open(string obj)
        {
            regionManager.Regions["Content"].RequestNavigate(obj);//这里需要依赖注入的方式
        }
    }

}

MainView.xaml    

<Window x:Class="WPFPrism.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:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:local="clr-namespace:WPFPrism"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">
    <!--自动关联AutoWireViewModel-->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" >
            <Button Content="打开模块A" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewA"/>
            <Button Content="打开模块B" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewB"/>
            <Button Content="打开模块C" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewC"/>
        </StackPanel>
        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="Content" />
    </Grid>
</Window>

ViewA.xaml                     B C同理

<UserControl x:Class="WPFPrism.Views.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:WPFPrism.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Background="Red">
    <Grid>
        <TextBlock Text="我是A" FontSize="50"/>
    </Grid>
</UserControl>

App.xaml  

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

    </Application.Resources>
</prism:PrismApplication>

App.xaml.cs 

using Prism.DryIoc;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WPFPrism.Views;

namespace WPFPrism
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            //通过容器去拿MainView,也是启动的地方
            return Container.Resolve<MainView>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //依赖注入的功能,增加依赖注入要实现的内容
            containerRegistry.RegisterForNavigation<ViewA>(); //containerRegistry.RegisterForNavigation<ViewA>("ViewA的具体名字")自定义,可以这样写;
            containerRegistry.RegisterForNavigation<ViewB>();
            containerRegistry.RegisterForNavigation<ViewC>();
        }
    }
}

来源:Prism框架学习_故里2130的博客-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/u012563853/article/details/124987710?spm=1001.2014.3001.5502

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值