一步步实现 Prism + MEF(一)--- 搭建框架

82 篇文章 12 订阅
53 篇文章 5 订阅

第一步:构建一个名为Bootstrapper的类作为引导程序。

    class Bootstrapper : MefBootstrapper
    {
    }

第二步:在MainWindow窗体中添加一个CoontentControl控件作为模块的容器,并在后台代码中添加[Export]属性以便MEF可以注入。

窗体代码:

<ContentControl prism:RegionManager.RegionName="MainRegion" />

后台代码:

复制代码

    using System.ComponentModel.Composition;

    [Export(typeof(MainWindow))]
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

复制代码

第三步:在Bootstrapper类中重写CreateShell方法以便返回一个Shell(MainWindow)实例。

        protected override DependencyObject CreateShell()
        {
            return Container.GetExportedValue<MainWindow>();
        }

第四步:在Bootstrapper类中重写InitializeShell方法以便启动Shell。

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }

第五步:在Bootstrapper类中重写ConfigureAggregateCatalog方法,将所有带有[Export]属性的类所在的程序集目录添加进去。

复制代码

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleAModule).Assembly));
        }

复制代码

第六步:创建一个类库项目MoudleA,添加类ModuleAModule并实现IModule接口,为该类添加[ModuleExport]属性以便让该类成为Prism模块初始化类。

复制代码

    [ModuleExport(typeof(ModuleAModule))]
    public class ModuleAModule : IModule
    {
        IRegionManager _regionManager;

        // 当Prism加载该模块时,它将通过MEF实例化该类,MEF将注入一个Region Manager实例
        [ImportingConstructor]
        public ModuleAModule(IRegionManager regionManager)
        {
            _regionManager = regionManager;
        }

        // 该方法将为模块启动提供一个代码入口点
        // 我们将把MEF容器里的ViewA注入到MainWindow界面定义的MainRegion中
        public void Initialize()
        {
            _regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewA));
        }
    }

复制代码

第七步:在ModuleA项目中添加ViewA用户控件,并在后台代码中添加[Export]属性,以便MEF在需要的时候能注入它。

窗口代码:

    <Grid>
        <!--  绑定ViewModel中的Title属性 -->
        <TextBlock Text="{Binding Title}" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
    </Grid>

后台代码:

复制代码

    [Export(typeof(ViewA))]
    public partial class ViewA : UserControl
    {
        public ViewA()
        {
            InitializeComponent();
        }
    }

复制代码

 第八步:在ModuleA项目中添加ViewAViewModel类

复制代码

    [Export(typeof(ViewAViewModel))]
    public class ViewAViewModel : BindableBase
    {
        private string _title = "Hello World";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        } 
    }

复制代码

完成上述步骤之后就可以在App.xaml.cs中直接运行Bootstrapper引导程序。

复制代码

       protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Bootstrapper bs = new Bootstrapper();
            bs.Run();
        }

复制代码

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值