Prism之WPF模块化入门(一)

       本文参考微软文档:https://msdn.microsoft.com/en-us/library/ff921141(v=pandp.40).aspx ,步骤与原文不一一对应。希望能帮助做C/S程序(WPF)的小伙伴们。 

    简单的介绍一下Prism,Prism的关注点是分离和松散耦合。Prism可帮助我们设计和构建具有重要表现和业务逻辑的应用程序。预计应用程序将在其生命周期内发生显着变化,以响应新的需求和业务机会。Prism构建的应用程序是“为了持久”而建立的,并且是为了改变而建立的。不需要这些特性的应用程序可能不会从使用Prism中受益。

    ⒈建立一个空白的解决方案命名为“HelloWorld.Desktop”。

    ⒉添加一个“新建项目”>“WPF应用”>命名为“HelloWorld.Desktop”。

    

    ⒊ 通过“管理NuGet程序包”,添加“Unity for Prism 6”、“Prism 6”、“Prism 6 for WPF”,如下图所示。

    

    添加完成之后,引入了如下dll,如下图所示

    

    ⒋将“MainWindow.xaml”重命名为“Shell”

    

    

<Window x:Class="HelloWorld.Desktop.Shell"
        xmlns:prism="http://www.codeplex.com/prism"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Hello World" Height="300" Width="300">
    <Grid>
        <ItemsControl x:Name="MainRegion" prism:RegionManager.RegionName="MainRegion"/>
    </Grid>
</Window>

      ⒌修改“App.xaml.cs”中的代码,添加代码如下 

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Bootstrapper bootstrapper=new Bootstrapper();
            bootstrapper.Run();
        }
    }

      删除 StartupUri="MainWindow.xaml"

<Application x:Class="HelloWorld.Desktop.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <!--StartupUri="MainWindow.xaml"-->
    </Application.Resources>
</Application>

      ⒍添加“Bootstrapper.cs” 类,代码如下:

    public class Bootstrapper:UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return new Shell();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.MainWindow = (Window) this.Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
        //下面代码为添加模块的代码。
            ModuleCatalog moduleCatalog = (ModuleCatalog) this.ModuleCatalog;
            moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
        }
    }

      ⒎添加新项目类库“HelloWorldModule”。

      ⒏添加文件夹“Services”、“ViewModels”、“Views”。

      ⒐在文件夹“Views”中添加用户控件“HelloWorldView.xaml”。

<UserControl x:Class="HelloWorldModule.Views.HelloWorldView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
            <TextBlock Text="Helllo World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"/>
    </Grid>
</UserControl>

      ⒑添加模块类“HelloWorldModule.cs”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism;
using Prism.Modularity;
using Prism.Regions;

namespace HelloWorldModule
{
    public class HelloWorldModule : IModule
    {
        private readonly IRegionManager regionManager;
        public void Initialize()
        {
            regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));
        }

        public HelloWorldModule(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
        }
    }
}

  

转载于:https://www.cnblogs.com/bjxingch/articles/7906537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值