Beispiel #1
0
        /// <summary>
        /// 以菜单形式加载插件
        /// <para>将<see cref="IPluginView"/>中的插件以<see cref="MenuStrip"/>的菜单形式来加载, 在点击菜单、窗口加载、窗口关闭时调用相应插件</para>
        /// </summary>
        /// <param name="menuStrip"></param>
        /// <param name="view"></param>
        /// <param name="form"></param>
        /// <param name="cmdData">插件命令的传入参数</param>
        /// <param name="bingdingPluginCommandEvents">插件命令事件处理方法</param>
        public static void LoadPluginView <TCmdData>(this MenuStrip menuStrip,
                                                     Form form, IPluginView view, TCmdData cmdData, Action <IPluginView> bingdingPluginCommandEvents)
        {
            try
            {
                var container = view.QueryContainer(menuStrip);
                menuStrip.LoadPluginContainer(container);
                container.LoadPluginCommands(cmdData);
                bingdingPluginCommandEvents(view);//绑定插件命令事件处理

                //在窗口加载时、窗口关闭时触发相应插件
                view.InvokePlugins(EPluginInvokeType.OnViewLoad, form);
                form.FormClosing += (sender, e) => view.InvokePlugins(EPluginInvokeType.OnViewClose, form);
            }
            catch (Exception ex) { MessageBoxEx.ShowError(ex); }
        }
Beispiel #2
0
        /// <summary>
        /// 向插件窗口的RibbonControl控件加载插件
        /// </summary>
        /// <param name="ribbonCtrl">加载插件的RibbonControl控件</param>
        /// <param name="caller">调用插件的控件,一般是窗体、菜单栏、RibbonControl、XtraTabControl、DcokControl、各类容器控件等</param>
        /// <param name="view">加载的插件视图</param>
        /// <param name="cmdData">命令输入参数</param>
        /// <param name="imageDir"></param>
        /// <param name="bindingEvent"></param>
        public static void LoadPlugins <TCmdData>(this RibbonControl ribbonCtrl, IPluginView view, object caller, TCmdData cmdData, string imageDir, Action <ICommand> bindingEvent)
        {
            var container = view.QueryContainer(ribbonCtrl.DefaultPageCategory.Name, ribbonCtrl.DefaultPageCategory.Text);

            var sbErrorCmds     = new StringBuilder();
            var imageCollection = ribbonCtrl.LargeImages == null ? new ImageCollection() : (ImageCollection)ribbonCtrl.LargeImages;

            ribbonCtrl.LargeImages = imageCollection;
            foreach (var pageContainer in container.SubContainers)//菜单页
            {
                var page = new RibbonPage {
                    Text = pageContainer.Text, Name = pageContainer.Name, Visible = pageContainer.Visible, Tag = pageContainer
                };
                ribbonCtrl.DefaultPageCategory.Pages.Add(page);
                foreach (var groupContainer in pageContainer.SubContainers)//菜单组
                {
                    var group = new RibbonPageGroup {
                        Text = groupContainer.Text, Name = groupContainer.Name, Visible = groupContainer.Visible, Tag = groupContainer
                    };
                    page.Groups.Add(group);
                    var plugins = groupContainer.Plugins.OrderBy(v => v.Index);
                    foreach (var plugin in plugins)//菜单项(插件项)
                    {
                        try
                        {
                            var item = PluginToButtonItem(plugin, imageCollection, caller, imageDir, cmdData);
                            bindingEvent(plugin.Command);
                            group.ItemLinks.Add(item);
                        }
                        catch (Exception ex) { sbErrorCmds.AppendLine($"程序集:{ plugin.AssemblyPath}\r\n命令:{plugin.TypeName}"); }
                        Application.DoEvents();
                    }
                }
            }
            if (sbErrorCmds.Length > 0)
            {
                sbErrorCmds.Insert(0, "以下插件命令创建失败(找不到程序集或命令,或加载过程出错,请注意程序集路径、名称、后缀(dll、exe)、类名是否正确):");
                throw new Exception(sbErrorCmds.ToString());
            }
        }