OSGi.NET使用笔记

一手资料来源于“开放工厂”,以下程序将会引用到一个核心文件UIShell.OSGi.dll

目前我对于OSGi这个框架的理解就是,主程序搜索并加载插件,以插件方式开放,便于扩展。


现在开始正式的旅程。

首先新建一个C#控制台应用程序(.NET 4.0)按照惯例取名HelloWorld

添加OSGi.NET的引用(上述dll文件),设置输出目录为bin(建议这样做)

这个程序的主函数(Main)中只做一件事,就是启动插件框架,完整代码如下

using System;
using UIShell.OSGi;

namespace OSGi.HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            using(BundleRuntime bdrt=new BundleRuntime())
            {
                Console.WriteLine("Start bundle...");
                bdrt.Start();
                bdrt.Stop();
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
    }
}

现在,我们的各项功能可以通过插件扩展实现。

给两个示例吧,一个是打印消息,一个是弹出消息框,都很简单。

编写插件需要输出为dll形式,因此我们选择新建Class(类库),同样要添加OSGi.NET的引用


两个类的代码分别如下

using System;
using UIShell.OSGi;

namespace PrintMessagePlugin
{
    public class PrintMessage:IBundleActivator
    {
        public void Start(IBundleContext context)
        {
            Console.WriteLine("<Start> Hello World  -- PrintMessage");
        }

        public void Stop(IBundleContext context)
        {
            Console.WriteLine("<Stop> See you next time. -- PrintMessage");
        }

    }
}

输出PrintMessage.dll



using System.Windows.Forms;
using UIShell.OSGi;

namespace PopupMsgBoxPlugin
{
    public class PopupMsgBox:IBundleActivator
    {
        public void Start(IBundleContext context)
        {
           MessageBox.Show("<Start> Hello World!","MsgBox");
        }

        public void Stop(IBundleContext context)
        {
            MessageBox.Show("<Stop> See you.","MsgBox");
        }
    }
}
输出PopupmsgBox.dll


为了能让主程序搜索并识别插件信息,我们需要添加清单文件

方法之一是 【项目(右键)】-->【添加(新建项...)】-->【数据(XML文件)】


将*.xml命名为manifest.xml然后根据类库代码编写内容,分别如下

PrintMessage的manifest.xml

<?xml version="1.0" encoding="utf-8" ?>
<Bundle xmlns="urn:uiosp-bundle-manifest-2.0"
        SymbolicName="PrintMessagePlugin"
        Name="PrintMessagePlugin"
        Version="1.0.0.0"
        InitializedState="Active" >
  <Activator Type="PrintMessagePlugin.PrintMessage" />
  <Runtime>
    <Assembly Path="PrintMessage.dll" />
  </Runtime>
</Bundle>


PopupMsgBox的manifest.xml
<?xml version="1.0" encoding="utf-8" ?>
<Bundle xmlns="urn:uiosp-bundle-manifest-2.0"
        SymbolicName="PopupMesgBoxPlugin"
        Name="PopupMsgBoxPlugin"
        Version="1.0.0.0"
        InitializedState="Active" >
  <Activator Type="PopupMsgBoxPlugin.PopupMsgBox" />
  <Runtime>
    <Assembly Path="PopupMsgBox.dll" />
  </Runtime>
</Bundle>

然后将生成的*.dll及对应的*.xml清单拷贝到HelloWorld主程序对应的bin/Plugins目录(没有请新建)

其中bin就是HelloWorld主程序的exe输出目录

这是bin目录下的内容


这是bin/Plugins目录下的内容

这两个文件夹下的内容如下



执行主程序HelloWorld,运行结果如下





本文原创,转载请注明出处

http://blog.csdn.net/fengyhack/article/details/40508635

转载于:https://www.cnblogs.com/fengyhack/p/10603650.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值