日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# WPF Caliburn.Micro框架下利用Mef加载其它项目界面

發(fā)布時間:2023/12/4 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# WPF Caliburn.Micro框架下利用Mef加载其它项目界面 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

01

前言

? ? MEF是微軟自家的托管可擴(kuò)展框架,在這里我把它用成了ioc容器。在Caliburn.Micro框架下,view和viewmodel被注入到CompositionContainer容器中,然后通過名稱可以實現(xiàn)view和viewmodel的匹配。利用這一特點,在多人合作項目開發(fā)中,一個解決方法就可以拆分成很多個項目,只用在主項目中搭建框架,每個分支項目開發(fā)好以后加載到容器中,就可以實現(xiàn)界面和邏輯的調(diào)用,可能這樣解釋有點生澀,具體我們看下面實例再去理解。

02


新建項目MefTest

第一步 :在我們的解決方法下添加新的項目MefTest(類庫)

第二步:MefTest下添加MefTestView.xaml和MefTestViewModel.cs

MefTestViewModel:

[Export(typeof(MefTestViewModel))] 一般是繼承公共的接口并導(dǎo)出,當(dāng)然像我這樣直接導(dǎo)出也是可以的.

//[Export("PluginTestViewModel", typeof(IPluggablePart))]//[PartCreationPolicy(CreationPolicy.Shared)]///也可以這樣[Export(typeof(MefTestViewModel))]//表示此類需要導(dǎo)出,導(dǎo)出的類型為objectpublic class MefTestViewModel{public void MefTestBtn(){MessageBox.Show("這是一個mef的測試類");}public int Sum(int a ,int b){return a + b;}}

MefTestView.xaml:

<UserControl x:Class="MefTest.MefTestView"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:MefTest"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><Grid ShowGridLines="True"><Grid.RowDefinitions><RowDefinition Height="*"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Button Name="MefTestBtn" Content="MefTestBtn" Background="LightCoral" FontSize="45"/></Grid> </UserControl>

03


通過Mef注入dll

詳細(xì)代碼如下:

DisplayRootViewFor<StartViewModel>();//顯示界面

這里也可以讓主界面的viewmodel繼承一個公共的接口,比如IShell,這樣這里接可以改寫為:

DisplayRootViewFor<IShell>();//顯示界面using Caliburn.Micro; using MefTest; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows;namespace CaliburnTest {class MyBootstrapper : BootstrapperBase{public MyBootstrapper(){Initialize();//初始化框架}//容器,裝東西用的。具體裝什么先不管。CompositionContainer container;//[Import]//public MefTestParts mefTestParts { get; set; }protected override void OnStartup(object sender, StartupEventArgs e){DisplayRootViewFor<StartViewModel>();//顯示界面}//private IDialogManager dialogManager = PlatformIoC.Get<IDialogManager>();//[Import(typeof(ContainerTest))]//public ContainerTest ts { get; set; }/// <summary>/// 方法1/// </summary>protected void Configure0(){var envirmentPath = System.IO.Directory.GetCurrentDirectory();//AssemblyCatalog 目錄的一種,表示在程序集中搜索var assemblyCatalog = new AssemblyCatalog(typeof(StartViewModel).Assembly);//此處這一句實際上沒啥用,因為此程序集下沒有任何我們需要的實例(各種handler)//在某個目錄下的dll中搜索。//var directoryCatalog = new DirectoryCatalog(@"C:\Program Files (x86)\YWTK\TOOLS\PLUGIN-LIBS\MISC\I12\", "*.dll");var directoryCatalog = new DirectoryCatalog(envirmentPath, @"ContainerDLL.dll");var aggregateCatalog = new AggregateCatalog(assemblyCatalog, directoryCatalog);//創(chuàng)建搜索到的部件,放到容器中。container = new CompositionContainer(aggregateCatalog);var batch = new CompositionBatch(); //如果還有自己的部件都加在這個地方batch.AddExportedValue<IWindowManager>(new WindowManager());batch.AddExportedValue<IEventAggregator>(new EventAggregator());batch.AddExportedValue(container);this.container.Compose(batch);}/// <summary>/// 方法2/// </summary>protected override void Configure(){var envirmentPath = System.IO.Directory.GetCurrentDirectory();AssemblySource.Instance.Add(Assembly.LoadFile(Path.Combine(envirmentPath, @"MefTest.dll")));//AssemblySource.Instance.Add(Assembly.LoadFile(Path.Combine(envirmentPath, @"PluginTest.dll")));var catalog = new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());this.container = new CompositionContainer(catalog);var batch = new CompositionBatch(); //如果還有自己的部件都加在這個地方batch.AddExportedValue<IWindowManager>(new WindowManager());batch.AddExportedValue<IEventAggregator>(new EventAggregator());batch.AddExportedValue(this.container);this.container.Compose(batch);}protected override object GetInstance(Type service, string key){if (service == null){return null;}string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;var exports = container.GetExportedValues<object>(contract);if (exports.Any()){return exports.First();}throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));}protected override IEnumerable<object> GetAllInstances(Type service){return container.GetExportedValues<object>(AttributedModelServices.GetContractName(service));}protected override void BuildUp(object instance){container.SatisfyImportsOnce(instance);}protected override void OnExit(object sender, EventArgs e){base.OnExit(sender, e);this.Application.Shutdown();}}}

04


主程序加載和調(diào)用

StartView.xaml中添加一個tab頁:

<TabItem x:Name="Up5" Header="MefTest" ><ContentControl cal:View.Model="{Binding MefTestView}"/></TabItem>

viewmodel中:

定義?MefTestViewModel

public MefTestViewModel MefTestView { get;set;}

然后在主程序的構(gòu)造函數(shù)中通過ioc獲取viewmodel實例:

MefTestView = IoC.Get<MefTestViewModel>();

這樣其它項目的界面就成功的被加載到了我們的主項目中,然而我們并沒有實例化,這樣如果我們定義了公共的接口,直接導(dǎo)出接口類型,就很好地實現(xiàn)了主項目和子項目的解耦。

05


運(yùn)行結(jié)果

總結(jié)

以上是生活随笔為你收集整理的C# WPF Caliburn.Micro框架下利用Mef加载其它项目界面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。