使用 Prism 构建插件 (.dll)

Using Prism for building a Plugin (.dll)

我正在开发一个插件,用于我使用的软件。软件插件通过附加从我的代码生成的 .dll 附加到软件。因此,该软件的文档要求您将某个 class(称之为 CPlugin)作为插件入口点。

我使用 Prism 阅读的所有教程都是您将项目作为 WPF 应用程序启动的地方。这样,您的项目将具有 App.xaml 和 App.cs 文件,您可以在其中开始实施 Prism 框架。编译代码(对于 WPF 应用程序)也会生成 .exe 而不是 .dll。

我设置插件的方式是使用 C# class 启动我的项目。然后我将创建我的 CPlugin class 并启动我的所有变量,然后显示我的 MainView,它创建我的 ViewModel 并从那里获取它。没有 App.xaml 或 App.cs。我不确定如何在我的限制条件下使用 Prism。

这是我正在为其开发插件的软件:https://www.csiamerica.com/products/etabs 在安装目录中安装;可以找到 API 帮助文件,其中解释了如何开发或启动插件。以下是相关信息的示例:

In your plugin, all functionality must be implemented in a class called cPlugin.

Class cPlugin must contain a subroutine cPlugin.Main that has two reference arguments of types ETABSv1.cSapModel and ETABSv1.cPluginCallback

还有

Adding a .NET Plugin The process for adding a .NET plugin is much simpler. In the External Plugin Data form, the user should simply browse for and select the plugin .NET DLL and click the "Add" button

这是显示空 window 的插件的一些示例代码: 创建一个 C# Class 库文件 (.NET Framework),引用 API 作为我的参考之一。

CPlugin.cs:

using Combonito.Views;          // Access Views (open window)

using CSiAPIv1;                  //to Access ETABS/SAP2000 API
using System;
using static Globals;

namespace Combonito
{
    // cPlugin has to be implemented in ETABS Plugins, it has to contain two functions Main() and Info()
    public class cPlugin
{

    private MainView _MyForm;

    //Entry point of plugin - has to exist with this exact signature
    // must call Finish() when plugin is closed!
    public void Main(ref cSapModel _SapModel, ref cPluginCallback _ISapPlugin)
    {
        ParentPluginObject = this;
        SapModel = _SapModel;
        ISapPlugin = _ISapPlugin;

        try
        {
            _MyForm = new MainView();   //Create MainView
            _MyForm.ShowDialog();       // Display window
        }
        catch (Exception ex)
        {
            try
            {
                ISapPlugin.Finish(1);
                Console.WriteLine(ex);
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1);
                throw;
            }
        }
    }

    // return value should be 0 if successful
    public int Info(ref string txt)
    {
        try
        {
            txt = "Plugin is written by Moustafa El-sawy (mk.elsawy@live.com)";
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            throw;
        }

        return 0;
    }

    //Deconstructor to clean up
    ~cPlugin()
    {
        Console.WriteLine(GC.GetGeneration(0));
    }
}

}

然后我有一个空的 window MainView 和一个视图模型 MainWindowViewModel。

编辑:这是启动任何插件的锅炉初始值,类似于我上面显示的,但有更多解释 https://kinson.io/post/etabs-plugin-quickstart/

你的起点应该是Bootstrapper

首先你需要安装:

  • Prism.Unity
  • Prism.Wpf

需要根据以下内容创建引导程序 https://github.com/PrismLibrary/Prism/blob/master/src/Wpf/Prism.Wpf/PrismBootstrapperBase.cs

覆盖虚拟方法以创建Shell(这是包含区域的主视图

重写虚拟方法来配置您的容器。 最后注册您的视图和视图模型

P.S.: 考虑为每个注册类型使用一个接口,例如我ShellViewModel

using Prism;
using Prism.Ioc;
using Prism.Unity;
using System.Windows;

namespace PrismTest
{
    public class Bootstrapper : PrismBootstrapperBase
    {
        protected override IContainerExtension CreateContainerExtension()
        {
            return new UnityContainerExtension();
        }

        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<Shell>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             containerRegistry.Register<IShellViewModel, ShellViewModel>();
        }
    }
}

从您的插件调用您的引导程序:

Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();

您的观点(Shell)

<Window
    x:Class="PrismTest.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:PrismTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="Shell"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <TextBox Text="{Binding MyText}" />
    </Grid>
</Window>

在 Shell.xaml.cs 中:使用您的视图模型。这个也可以自动注入

public partial class Shell : Window
{
    private readonly IUnityContainer _container;
    public Shell(IUnityContainer container)
    {
        InitializeComponent();

        _container = container;

        this.DataContext = _container.Resolve<IShellViewModel>();

    }
}

你的视图模型

public class ShellViewModel : BindableBase, IShellViewModel
{
    private string m_MyText = "Shell ViewModel Text";

    public string MyText
    {
        get { return m_MyText; }
        set { SetProperty(ref m_MyText, value); }
    }
}

您的界面:

internal interface IShellViewModel
{
   string MyText { get; set; }
}

结果视图;