[Songqw.Net 基礎]WPF實現簡單的外掛化開發
阿新 • • 發佈:2018-11-07
原文:
[Songqw.Net 基礎]WPF實現簡單的外掛化開發
MainWindow.xaml.cs:
接著上一篇部落格, 那裡實現了簡單的控制檯載入外掛,在這裡通過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 方法傳遞引數, 進行賦值等操作. 資源釋放方面需要額外注意....