一步步實現 Prism + MEF(一)--- 搭建框架


第一步:構建一個名為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();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM