15 Prism框架--区域

功能

通过点击不同的Button,实现显示不同的界面

View部分

MainWindow.xaml

  • 两个<Grid.RowDefinitions/>​,上面放Button​,下面放导航页面。导航页面用<ContentControl/>​介绍ContentControl1

<Window
    x:Class="FullApp.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    Title="{Binding Title}"
    Width="525"
    Height="350"
    prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewA"
                Content="打开模块A" />
            <Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewB"
                Content="打开模块B" />
            <Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewC"
                Content="打开模块C" />
        </StackPanel>
        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion"/>
    </Grid>
</Window>

创界导航页面

ViewB​、ViewC​和ViewA​类似。

<UserControl x:Class="FullApp.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:FullApp.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="我是模块A"
                   FontSize="80" />
    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FullApp.Views
{
    /// <summary>
    /// ViewA.xaml 的交互逻辑
    /// </summary>
    public partial class ViewA : UserControl
    {
        public ViewA()
        {
            InitializeComponent();
        }
    }
}

ViewModels

  1. 创建命令
   public DelegateCommand<string> OpenCommand { get; private set; }
  1. MainWindowViewModel的构造函数要用到第一步创建的命令,传进一个函数Open
 private readonly IRegionManager regionManager;
        public MainWindowViewModel(IRegionManager regionManger)
        {
            OpenCommand = new DelegateCommand<string>(Open);
            this.regionManager = regionManger;
        }

这个地方参数为regionManger,是View部分xmal prism:RegionManager.RegionName="ContentRegion"末尾部分的区域

  1. Open​依赖注入(DI)2
    private void Open(string obj)
        {
            //首先通过IregionManager接口获取全局定义的区域
            //往这个区域动态的去设置内容
            //设置内容的方式是依赖注入
            regionManager.Regions["ContentRegion"].RequestNavigate(obj);
        }

这里用了依赖注入,如果不用依赖注入Open里面将会实例多个导航界面,导航界面和本类形成依赖

  1. 依赖注入注册,在APP.xaml​里实现
using FullApp.Views;
using Prism.DryIoc;
using Prism.Ioc;
using System.Windows;

namespace FullApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App:PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<ViewA>("ViewA");
            containerRegistry.RegisterForNavigation<ViewB>("ViewB");
            containerRegistry.RegisterForNavigation<ViewC>("ViewB");
        }
    }
}


  1. 介绍ContentControl

    ContentControl控件是一种可以添加和自定义的控件,用于在模板、表单和文档中包含特定类型的内容[1](https://support.microsoft.com/en-us/office/about-content-controls-283b1e29-0b77-4781-b236-2d02c1cce1c2)[2](https://learn.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-content-controls)。例如,你可以使用ContentControl控件来创建一个下拉列表,让用户从有限的选项中选择[1](https://support.microsoft.com/en-us/office/about-content-controls-283b1e29-0b77-4781-b236-2d02c1cce1c2)

    ContentControl控件可以包含任意类型的内容,如日期、列表或格式化文本[2](https://learn.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-content-controls)。你可以通过设置ContentControl控件的属性来控制其外观和行为,如标题、标签、颜色、锁定等[1](https://support.microsoft.com/en-us/office/about-content-controls-283b1e29-0b77-4781-b236-2d02c1cce1c2)[2](https://learn.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-content-controls)

    在WPF中,ContentControl控件是一个基类,有很多派生类,如Button、Label、CheckBox等[3](https://blog.csdn.net/BYH371256/article/details/125513486)[4](https://zhuanlan.zhihu.com/p/358449148)。你可以使用XAML或代码来创建和操作ContentControl控件[3](https://blog.csdn.net/BYH371256/article/details/125513486)[4](https://zhuanlan.zhihu.com/p/358449148)[5](https://zhuanlan.zhihu.com/p/514311839)

    在Word中,ContentControl对象是一个代表文档中内容控件的对象,你可以使用VBA来插入、删除、修改或查询内容控件的属性和方法[6](https://learn.microsoft.com/zh-cn/office/vba/api/word.contentcontrol)

    ↩︎

  2. 依赖注入(DI)

    定义:

    依赖注入(DI)是一种软件设计模式,它可以实现控制反转(IoC),即让类的依赖项由外部提供,而不是在类内部创建[1](https://www.malema.net/csharp-di/intro.html)[2](https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection)。这样可以降低类之间的耦合,提高代码的可测试性和可维护性[1](https://www.malema.net/csharp-di/intro.html)[2](https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection)

    C#中有很多支持依赖注入的框架,如Autofac、Ninject、Unity等[3](https://www.zhihu.com/question/27181326)。你也可以使用.NET自带的依赖注入机制,通过IServiceCollection接口来注册服务,并通过IServiceProvider接口来获取服务[2](https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection)[4](https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0)[5](https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection-usage)

    在ASP.NET Core中,依赖注入是一个内置的功能,你可以在Startup类中配置服务,并在控制器、过滤器、视图等地方使用构造函数注入或属性注入的方式来获取服务[6](https://blog.csdn.net/qq_39173779/article/details/129476121)[4](https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0)

    如果你想了解更多关于C#中依赖注入的内容,请参考以下链接:

    DI:Dependention Injection

    还有个DI叫依赖反转Dependention Inversion pre...

    一般用反射不会这么用,都是用封装好的反射,又叫依赖注入。

    依赖注入框架:Nuget–DependencyInjection

    添加名称空间:using Microsolft.Extensions.DependencyInjection

    typeof

    image.png

    依赖注入有个很重要的容器ServiceProvider

    var sc new ServiceCollection();
    

    这就是个容器。

    sc.AddScoped(typeof(ITank),typeof(MediumTank));
    

    把一对类型放进了容器

    分割线以上是一次性注册,分割线以下代表在程序其他任何地方都可以用。 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值