C# WPF通过反射及Ioc容器综合实例

概述

这节主要通过发射+Caliburn.Micro自带的ioc容器实现加载并显示其它项目中的界面.实现效果如下:

具体实现

-. ①首先在引导程序页面通过发射加载类库,并将视图注入ioc容器,这里为了实现解耦合注入了ICommonBasePage接口类型:

 private void ExternalLoad() { //container = new SimpleContainer(); //container.Instance(container);
var asmPath = Assembly.GetAssembly(typeof(HelloBootstrapper)).Location; var path = Directory.GetParent(asmPath).FullName; string filePath = Path.Combine(path, "PluginTest.dll");
//var assembly = Assembly.GetAssembly(typeof(PageBase)); //container.AllTypesOf(assembly); var assembly = Assembly.LoadFile(filePath); assembly.GetTypes() .Where(type => type.IsClass) .Where(type => type.Name.EndsWith("ViewModel")) .ToList() .ForEach(viewModelType => container.RegisterPerRequest( typeof(ICommonBasePage), viewModelType.ToString(), viewModelType));
//container // .Singleton() // .Singleton() // .PerRequest(); }

接口定义在单独的类库中:

 public interface ICommonBasePage { void ShowNewWindow(); }

加入程序集到IEnumerable

protected override IEnumerable SelectAssemblies() { string filePath = Path.Combine(Directory.GetCurrentDirectory(), "PluginTest.dll");
var assembly = Assembly.LoadFile(filePath); return new[] { Assembly.GetExecutingAssembly(), assembly }; }


-.②PluginTest是要加载显示的项目:

PluginTestViewModel.cs源码如下:

using CommonShareBase;using System.ComponentModel.Composition;using System.Windows;
namespace PluginTest{ //[PluggablePart(name: "PluginTestViewModel", desciption: "This is PluginTestViewModel content pluggable part.", displayNameResourceKey: "PluginTestViewModel")] //[Export("PluginTestViewModel", typeof(IPluggablePart))] //[PartCreationPolicy(CreationPolicy.Shared)] ///也可以这样 [Export(typeof(ICommonBasePage))] public class PluginTestViewModel : ICommonBasePage { //public void PreparePart(IPluggablePartContext pluggablePartContext) //{ // //throw new NotImplementedException(); //}
public void ShowNewWindow() { MessageBox.Show("dotnet讲堂"); } }}

PluginTestView.xaml代码如下:

<UserControl x:Class="PluginTest.PluginTestView" 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:PluginTest" 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="ShowNewWindow" Content="ShowNewWindow" Grid.Row="0" Grid.Column="0" FontSize="35" Background="LightBlue"/> Grid>UserControl>


-. ③在调用的地方先定义ICommonBasePage 类型的VM

 public ICommonBasePage PluginTestViewModel { get; set; }

按钮事件里面从ioc获取视图并给界面赋值:

 public void IocTest4() { //通过接口实现解耦合 var shellVM = IoC.Get(); //shellVM.ShowNewWindow();
PluginTestViewModel = shellVM; ActivateItemAsync(PluginTestViewModel); }

源码下载

链接:https://pan.baidu.com/s/1wtNduWVUiFV46WwWE4hrEw

提取码:6666

展开阅读全文

页面更新:2024-05-04

标签:容器   赋值   视图   反射   源码   实例   加载   接口   界面   定义   程序   项目

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top