原文:[Songqw.Net 基础]WPF实现简单的插件化开发

版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/article/details/50895733

接着上一篇博客, 那里实现了简单的控制台加载插件,在这里通过WPF实现,做个备份.

WPF控件空间经常会与WinFrom混淆,要记得WPF控件是引用 using System.Windows.Controls;

1.构建控件:

WpfPart1.xaml

<UserControl x:Class="Songqw.Net.Plugins.Test.WPFPlugins.WpfPart1"
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>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF7C44F5" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Grid.Background> </Grid>
</UserControl>

 

WpfPart1.xaml.cs

using System;
using System.Windows.Controls;
using Songqw.Net.Plugins.Interface; namespace Songqw.Net.Plugins.Test.WPFPlugins
{
/// <summary>
/// WpfPart1.xaml 的交互逻辑
/// </summary>
public partial class WpfPart1 : UserControl, IPluginMember
{
public WpfPart1()
{
InitializeComponent();
} public string PluginName()
{
return "WpfPart1";
} public int PluginId()
{
return GetHashCode();
} public object InitPlugin()
{
return this;
} public object InitPlugin(object[] paras)
{
return this;
} public bool DisposePlugin()
{
throw new NotImplementedException();
}
}
}

WpfPart2 同上, 修改下名称即可.

测试WPF程序:

MainWindow.xaml

<Window x:Class="Songqw.Net.Plugins.Test.WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF插件测试程序" Height="350" Width="525">
<Grid> <Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="100*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions> <StackPanel x:Name="PartPanel" Margin="0,4,0,2" Grid.Row="1"/> <Grid x:Name="PartGrid"
HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="397"
Grid.Column="1"
Grid.Row="0"
Grid.RowSpan="3" Background="#FF686868"/> <Label Content="加载插件数量 : 0 "
Name="NumLabel"
Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Top" Height="20" Padding="5,0" Margin="5,0"/> <Button Content="选择目录"
Grid.Row="2"
Grid.Column="0"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="21,3,0,0" Width="75" Click="Button_Click"
/> </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Windows;
using System.Windows.Forms;
using Songqw.Net.Plugins.Interface;
using Songqw.Net.Plugins.Tool;
using Application = System.Windows.Forms.Application;
using Button = System.Windows.Controls.Button; namespace Songqw.Net.Plugins.Test.WpfApplication
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
var folderBrowserDialog = new FolderBrowserDialog(); folderBrowserDialog.SelectedPath = Application.StartupPath; folderBrowserDialog.ShowDialog(); if (!string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
{
PartGrid.Children.Clear();
PartPanel.Children.Clear(); var plugins = LoadLibrary.LoadDocument(Application.StartupPath); if (plugins != null && plugins.Length > 0)
{
foreach (Type t in plugins)
{
var instance = LoadLibrary.InitClassByType(t) as IPluginMember; if (instance == null)
continue; var btn = new System.Windows.Controls.Button
{
Tag = instance,
Content = instance.PluginName(),
Height = 25
}; btn.Click += btn_Click;
PartPanel.Children.Add(btn);
}
NumLabel.Content = string.Format("加载插件数量 : {0}", plugins.Length);
}
}
} void btn_Click(object sender, RoutedEventArgs e)
{
PartGrid.Children.Clear();
var btn = sender as Button;
var instance = btn.Tag as IPluginMember;
var ui = instance.InitPlugin();
var uiElement = ui as UIElement;
PartGrid.Children.Add(uiElement);
}
}
}

测试效果:

实际使用时, 可以通过InitPlugin 方法传递参数, 进行赋值等操作.   资源释放方面需要额外注意....

[Songqw.Net 基础]WPF实现简单的插件化开发的更多相关文章

  1. [Songqw.Net 基础]WPF插件化中同步Style

    原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/a ...

  2. 零基础ASP.NET Core MVC插件式开发

    零基础ASP.NET Core MVC插件式开发 一个项目随着业务模块的不断增加,系统会越来越庞大.如果参与开发的人员越多,管理起来也难度也很大.面对这样的情况,首先想到的是模块化插件式开发,根据业务 ...

  3. NET 平台下的插件化开发内核

    .NET 平台下的插件化开发内核(Rabbit Kernel)   每个程序猿都有一个框架梦,曾经在2013年8月15日写过一篇“Koala Framework是什么?我为什么要写这个框架?”的文章, ...

  4. 《Android插件化开发指南》面世

    本书在京东购买地址:https://item.jd.com/31178047689.html 本书Q群:389329264 (一)这是一本什么书 如果只把本书当作纯粹介绍Android插件化技术的书籍 ...

  5. Android 插件化开发(一):Java 反射技术介绍

    写在前面:学习插件化开发推荐书籍<Android 插件化开发指南>,本系列博客所整理知识部分内容出自此书. 在之前的项目架构的博文中,我们提到了项目插件化架构,提到插件化架构不得不提的到J ...

  6. apisix docker镜像构建及插件化开发

    高能劝退:lua开发,适合小白看!!! 前段时间有个项目,用的java程序做网关,压测tps只有1k多点,惨不忍睹. 后来公司有个大佬改用apisix做网关,tps飙升到1w多. 于是对神奇的apis ...

  7. Android组件化和插件化开发

    http://www.cnblogs.com/android-blogs/p/5703355.html 什么是组件化和插件化? 组件化开发就是将一个app分成多个模块,每个模块都是一个组件(Modul ...

  8. Android插件化开发

    客户端开发给人的印象往往是小巧,快速奔跑.但随着产品的发展,目前产生了大量的门户型客户端.功能模块持续集成,开发人员迅速增长.不同的开发小组开发不同的功能模块,甚至还有其他客户端集成进入.能做到功能模 ...

  9. JavaScript插件化开发

    大熊君JavaScript插件化开发 一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得昨天的那篇文章吗------这个系列的开篇(第一季).主要讲述了以“jQuery的方式如何开发插件”, 那 ...

随机推荐

  1. nginx 代理服务器

    目前现状:只有1个机器能上网(web),其他机器不能方法:能上网的做一个代理web服务器中转,其他机器连接它即可。采用nginxNginx配置如下:server{        resolver 8. ...

  2. [Tools] Fix Only Committed Files with Prettier and lint-staged

    In this lesson we'll use prettier and lint-staged to run prettier only on files that have been chang ...

  3. 【27.77%】【BZOJ 4066】简单题

    Time Limit: 50 Sec  Memory Limit: 20 MB Submit: 1919  Solved: 533 [Submit][Status][Discuss] Descript ...

  4. js进阶正则表达式11RegExp的属性和方法(RegExp的属性和方法,就是RegExp对象.(点)什么的形式)(正则表达式执行之前会被编译)

    js进阶正则表达式11RegExp的属性和方法(RegExp的属性和方法,就是RegExp对象.(点)什么的形式)(正则表达式执行之前会被编译) 一.总结 1. RegExp的属性和方法,就是RegE ...

  5. css 水平垂直居中的方法总结

    在项目中经常会遇到设置元素水平垂直居中的需求.而且具体的场景也不同,所以将个人总结的方法做个汇总,希望对浏览者有用. 以下所举的例子都以一个html为准,这里规定好一些公用样式. body { bac ...

  6. 设置好ftp后用xftp连接提示无法打开,无法显示远程文件夹

    原文:设置好ftp后用xftp连接提示无法打开,无法显示远程文件夹 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/the_victory/artic ...

  7. PEM_密钥对生成与读取方法

    PS:欢迎转载,但请注明出处,谢谢配合. 前言: PEM是OpenSSL和许多其他SSL工具的标准格式,OpenSSL 使用PEM 文件格式存储证书和密钥.这种格式被设计用来安全的包含在ascii甚至 ...

  8. web报表工具FineReport经常使用函数的使用方法总结(文本函数)

    文本函数 CHAR CHAR(number):依据指定数字返回相应的字符.CHAR函数可将计算机其它类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...

  9. [tmux] Create collections of panes using tmux windows

    In tmux, a window is a collection of panes. Creating multiple windows is a great way to organize you ...

  10. Linux开发环境搭建与使用系列教程

    00.Linux开发环境搭建与使用1——Linux简史 01.Linux开发环境搭建与使用2——Linux系统(ubuntu)安装方案 02.Linux开发环境搭建与使用3——通过虚拟机安装系统(ub ...