浏览:1802008-04-10 15:41   来自deerchao      :
Flexible Framework是一个插件框架,它唯一的功能就是管理大家开发的各种插件.

那么插件是什么?

插件可以是为最终用户提供实际功能的代码.比如一个列出"我的电脑"里目录树,并能添加/删除目录的FolderView插件;或者一个像QQ那样的IM插件;或者一个能自动下载字幕的媒体播放器插件,等等等等.

插件也可以是为其它插件提供基础服务的代码.比如一个为其它插件提供Log服务的Logger插件,或者为FolderView提供目录删除/创建/重命名信息的DirectoryWather插件等等.

为什么要使用插件结构,而不是直接把代码组合到一起?

一方面使用插件结构之后可以让程序拥有更好的结构,减少不必要的相互依赖(耦合),从而提高软件的可维护性;
另一方面,用户也获得了更多的灵活性,他可以只启用对他有用的部分插件,而不必为了某个小功能而等待30秒钟的软件加载时间.
把软件分解为不同的插件之后,某些公共的功能就可以只做一次(比如用户设置的保存,快捷键的设置与使用等),从而做得更好.

Flexible Framework里最简单的插件是什么样的?

实际上,这个不是最简单的,因为它还展示了怎么使用UserSetting...

using System;
using System.Collections.Generic;
using Flexible.Implementions.Bases;
using Flexible.Implementions.Attributes;
using Flexible.Interfaces;

namespace Flexible.Samples
{
    public class HelloWorldPlugin : PluginBase
    {
//Enable和Disable是所有插件都必须实现的接口
//
IPluginActivateContext用于提供当前插件依赖的或感兴趣的其它插件的实例
//HelloWorldPlugin不依赖于其它插件,对其它插件也不感任何兴趣,所以可以不理它
        public override void Enable(IPluginActivateContext context)
        {
            for (int i = 0; i < TimesToSay; i++)
                Console.WriteLine("Hello, {0}!", Username);
        }

        public override void Disable(IPluginActivateContext context)
        {
            Console.WriteLine("Goodbye, {0}!", Username);
        }
//下面是此插件的签名,会被最终用户看到
        public override string Name
        {
            get { return "Hello world"; }
        }

        public override string Description
        {
            get { return "Sample plugin."; }
        }

        public override string Author
        {
            get { return "deerchao"; }
        }

        public override DateTime ReleaseTime
        {
            get { return new DateTime(2008, 4, 7); }
        }
//下面是此插件的版本信息,用于处理版本兼容性
        public override string Id
        {
            get { return "Flexible.Samples.HelloWorld"; }
        }

        public override Version Version
        {
            get { return new Version(1, 0, 0, 0); }
        }

        public override Version CompatibleVersion
        {
            get { return new Version(1, 0, 0, 0); }
        }
//以下是用户可修改的设置
        [UserSetting("Times to say", null, "How many times of hello to say.", 1)]
        public int TimesToSay
        {
            get;
            set;
        }

        [UserSetting("Username", null, "Name of the current user.","User")]
        public string Username
        {
            get;
            set;
        }
    }
}


楼主
  3个月前   deerchao      :
展示一个依赖于其它插件的插件:

下面是个Menu插件,依赖于Window插件:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Flexible.Implementions.Bases;
using Flexible.Interfaces;

namespace Flexible.Wpf
{
public class MenuPlugin : PluginBase
{
public MenuPlugin()
{
exit = new MenuItem();
exit.Header = "E_xit";
exit.Click += (sender, e) => Application.Current.Shutdown();

file = new MenuItem();
file.Header = "_File";
file.Items.Add(exit);

menu = new Menu();
menu.Items.Add(file);
}

readonly Menu menu;
readonly MenuItem file;
readonly MenuItem exit;

public Menu Menu
{
get { return menu; }
}

public MenuItem File
{
get { return file; }
}

public MenuItem Exit
{
get { return exit; }
}

public override void Enable(IPluginActivateContext context)
{
WindowPlugin win = context.GetRequirement("Flexible.Wpf.Window") as WindowPlugin;
win.MainRegion.Children.Add(menu);
}

public override void Disable(IPluginActivateContext context)
{
WindowPlugin win = context.GetRequirement("Flexible.Wpf.Window") as WindowPlugin;
win.MainRegion.Children.Remove(menu);
}

public override IEnumerable Requirements
{
get
{
yield return new PluginQuery("Flexible.Wpf.Window", new Version(1, 0, 0, 0));
}
}

#region Info
public override string Name
{
get { return "Menu"; }
}

public override string Description
{
get { return "Main Menu"; }
}

public override string Author
{
get { return "deerchao"; }
}

public override DateTime ReleaseTime
{
get { return new DateTime(2008, 4, 9); }
}

public override string Id
{
get { return "Flexible.Wpf.Menu"; }
}

public override Version Version
{
get { return new Version(1, 0, 0, 0); }
}

public override Version CompatibleVersion
{
get { return new Version(1, 0, 0, 0); }
}
#endregion
}
}


对应的Window插件:


using System;
using System.Windows;
using Flexible.Implementions.Bases;
using Flexible.Interfaces;
using System.Windows.Controls;

namespace Flexible.Wpf
{
public class WindowPlugin : PluginBase, IApplication
{
public WindowPlugin()
{
}

Window window;
StackPanel mainRegion;
Application application;

public Window Window
{
get { return window; }
}

public Application Application
{
get { return application; }
}

public StackPanel MainRegion
{
get { return mainRegion; }
}

public override void Enable(IPluginActivateContext context)
{
mainRegion = new StackPanel();
window = new Window();
window.Content = mainRegion;
application = new Application();
}

public override void Disable(IPluginActivateContext context)
{
application.Shutdown();
}

#region Info
public override string Name
{
get { return "Main Window"; }
}

public override string Description
{
get { return "Main window of the application."; }
}

public override string Author
{
get { return "deerchao"; }
}

public override DateTime ReleaseTime
{
get { return new DateTime(2008, 4, 9); }
}

public override string Id
{
get { return "Flexible.Wpf.Window"; }
}

public override Version Version
{
get { return new Version(1, 0, 0, 0); }
}

public override Version CompatibleVersion
{
get { return new Version(1, 0, 0, 0); }
}
#endregion

#region IApplication Members

public bool Run()
{
application.Run(window);
return true;
}

#endregion
}
}
回复  1楼 回到顶楼 

你还不是小组成员,加入小组以后才能发布新主题!
1 17317