15 Prism框架

编程入门 行业动态 更新时间:2024-10-27 16:26:59

15 Prism<a href=https://www.elefans.com/category/jswz/34/1770644.html style=框架"/>

15 Prism框架

功能

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

View部分

MainWindow.xaml

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

<Windowx:Class="FullApp.Views.MainWindow"xmlns=""xmlns:x=""xmlns:prism="/"Title="{Binding Title}"Width="525"Height="350"prism:ViewModelLocator.AutoWireViewModel="True"><Grid><Grid.RowDefinitions><RowDefinition Height="auto" /><RowDefinition/></Grid.RowDefinitions><StackPanel Orientation="Horizontal"><ButtonMargin="5"Command="{Binding OpenCommand}"CommandParameter="ViewA"Content="打开模块A" /><ButtonMargin="5"Command="{Binding OpenCommand}"CommandParameter="ViewB"Content="打开模块B" /><ButtonMargin="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=""xmlns:x=""xmlns:mc="" xmlns:d="" 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]()[2]()​。例如,你可以使用ContentControl控件来创建一个下拉列表,让用户从有限的选项中选择​[1]()

    ContentControl控件可以包含任意类型的内容,如日期、列表或格式化文本​[2]()​。你可以通过设置ContentControl控件的属性来控制其外观和行为,如标题、标签、颜色、锁定等​[1]()[2]()

    在WPF中,ContentControl控件是一个基类,有很多派生类,如Button、Label、CheckBox等​[3]()[4]()​。你可以使用XAML或代码来创建和操作ContentControl控件​[3]()[4]()[5]()

    在Word中,ContentControl对象是一个代表文档中内容控件的对象,你可以使用VBA来插入、删除、修改或查询内容控件的属性和方法​[6](.contentcontrol)

    ‍ ↩︎

  2. 依赖注入(DI)

    定义:

    依赖注入(DI)是一种软件设计模式,它可以实现控制反转(IoC),即让类的依赖项由外部提供,而不是在类内部创建​[1](.html)[2]()​。这样可以降低类之间的耦合,提高代码的可测试性和可维护性​[1](.html)[2]()

    C#中有很多支持依赖注入的框架,如Autofac、Ninject、Unity等​[3]()​。你也可以使用.NET自带的依赖注入机制,通过IServiceCollection接口来注册服务,并通过IServiceProvider接口来获取服务​[2]()[4](=aspnetcore-6.0)[5]()

    在ASP.NET Core中,依赖注入是一个内置的功能,你可以在Startup类中配置服务,并在控制器、过滤器、视图等地方使用构造函数注入或属性注入的方式来获取服务​[6]()[4](=aspnetcore-6.0)

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

    • 《C# 依赖注入介绍》​[1](.html)
    • 《.NET 中的依赖关系注入》​[2]()
    • 《ASP.NET Core 依赖注入》​[4](=aspnetcore-6.0)
    • 《使用依赖关系注入》​[5]()

    DI:Dependention Injection

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

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

    依赖注入框架:Nuget–DependencyInjection

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

    typeof

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

    var sc new ServiceCollection();
    

    这就是个容器。

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

    把一对类型放进了容器

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

更多推荐

15 Prism框架

本文发布于:2024-03-09 08:27:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1724464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   Prism

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!