如何将Excel-DNA中的CustomUI标签与OnAction方法分开?

时间:2014-02-06 11:51:06

标签: excel-dna

在我的.dna文件中,我有:

<DnaLibrary Name="First Add-In" RuntimeVersion="v4.0" Language="C#">
  <ExternalLibrary Path="MyLibrary.dll" Pack="true"/>  
  <Image Name="M" Path="M.png" Pack="true" />
  <CustomUI>
    <customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui' loadImage='LoadImage'>
      <ribbon>
        <tabs>
          <tab id='CustomTab' label='My 2010 Tab'>
            <group id='SampleGroup' label='My Sample Group'>
              <button id='Button1' label='My Second Button' image='M' size='normal' onAction='RunTagMacro' tag='ReformatSelection='/>
            </group >
          </tab>
        </tabs>
      </ribbon>
    </customUI>
  </CustomUI>  
</DnaLibrary>

在我的.cs文件中,我有:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExcelDna.Integration;
using System.Runtime.InteropServices;
using ExcelDna.Integration.CustomUI;
using System.Windows.Forms;

namespace MyLibrary
{
    [ComVisible(true)]
    public class Class1 : ExcelRibbon
    {                

        public void ReformatSelection(IRibbonControl control)
        {
            MessageBox.Show("Hello");
        } 
    }
}

当我加载插件时,按钮和选项卡在功能区中显示正常,但单击该按钮不会运行ReformatSelection方法。在随Excel-DNA提供的示例文件中,挂接到onAction事件的所有子函数和函数都在.dna文件中。我试图将它们从.dna文件移到.cs文件中。我做错了什么?

1 个答案:

答案 0 :(得分:1)

ReformatSelection()的签名不适合功能区按钮的onAction处理程序。

应该是:

public void ReformatSelection(IRibbonControl control) {...}

您可以在此处获取所有Office功能区回调签名的列表:http://msdn.microsoft.com/en-us/library/aa722523(v=office.12).aspx