checkbox的OnAction不会做任何事情

我有一个菜单中的多个checkbox的自定义function区:

<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load"> <ribbon> <tabs> <tab id="ribbon" label="Ribbon"> <group id="ribbonGroup" label="Group"> <menu id="menu" label="Menu"> <checkBox id="checkbox1" label="Checkbox 1" visible="true" onAction="OnCheckboxChanged"/> <checkBox id="checkbox2" label="Checkbox 2" visible="true" onAction="OnCheckboxChanged"/> <checkBox id="checkbox2" label="Checkbox 2" visible="true" onAction="OnCheckboxChanged"/> </group> </tab> </tabs> </ribbon> </customUI> 

这是相应的C#代码:

 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Drawing; using System.Windows.Forms; using Office = Microsoft.Office.Core; using Excel = Microsoft.Office.Interop.Excel; using Microsoft.Office.Tools.Excel; namespace ExcelAddIn1 { [ComVisible(true)] public class SSRRibbon : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public SSRRibbon() { } #region IRibbonExtensibility-Member public string GetCustomUI(string ribbonID) { return GetResourceText("ExcelAddIn1.SSRRibbon.xml"); } #endregion #region ribbon callback functions public void Ribbon_Load(Office.IRibbonUI ribbonUI) { this.ribbon = ribbonUI; } public void OnCheckboxChanged(Office.IRibbonControl control) { int i = 1; } #endregion #region auxiliary private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion } } 

但是, OnCheckboxChanged永远不会被调用。 当我用button使用这个callback函数的时候,它工作的很好,但是它没有checkbox,既不在菜单中,也不直接在function区组中。 它也适用于getPressed而不是onAction

这个线程和这个社交MSDN线程都表示onAction在参数中有第二个参数; isPressed

 public void markAsRead_OnAction(Office.IRibbonControl control, bool isPressed) { public void cbViewAction(Office.IRibbonControl control, bool pressed) { 

这也是官方文件规定的做法 :

 public void OnActionCallback(Office.IRibbonControl control, bool isPressed) { if (control.Id == "checkBox1") { MessageBox.Show("You clicked " + control.Id); } else { MessageBox.Show("You clicked a different control."); } } 

定义callback方法

  1. 它必须被宣布为公开的。
  2. 其名称必须与您在Ribbon XML文件中分配给控件的callback方法的名称匹配。
  3. 其签名必须与可用于相关function区控件的一种callback方法的签名相匹配。

继这篇文章之后,我阅读了为开发人员定制2007 Office Fluentfunction区(第3部分)

我可以有两个具有相同名称但签名不同的callback吗?

尽pipe可以这样做,但是我们build议您为每个控件使用不同的callback函数(不要指望内置重载来处理两个callback函数之间的区别)。 例如,假设您使用两个相同名称的callback函数编写Fluent UI加载项,如下面的代码所示。

 public void doSomething(IRibbonControl control, bool pressState); public void doSomething(IRibbonControl control); 

另外假定你的XML标记定义了一个toggleButton控件和一个button控件,并且它们每个都有一个onAction =“doSomething”的callback函数。 在这种情况下,只有toggleButton控件可以工作,因为Visual Basic和Visual C#自动生成的IDispatch实现。 如果你自己编写一个C ++插件并实现IDispatch,这种情况将会起作用。 (换句话说,最好不要这样做。)