我遇到了关于 CustomTaskPaneFactory
的最奇怪的问题在Excel-DNA 0.32
.
CustomTaskPaneFactory
当我使用继承自模板化任务 Pane ( MyIntCTP
) 的类 ( MyCTP<T>
) 创建任务 Pane 时抛出异常。
这是一个说明上下文的示例:
<DnaLibrary RuntimeVersion="v4.0" Language="CS">
<Reference Path="System.Windows.Forms.dll" />
<![CDATA[
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
using System.Windows.Forms;
internal class ThisAddIn : IExcelAddIn
{
public void AutoOpen()
{
var p = CustomTaskPaneFactory.CreateCustomTaskPane(typeof(MyIntCTP), "o");
p.Visible = true;
}
public void AutoClose()
{ }
}
public class MyCTP<T> : UserControl
{ }
public class MyIntCTP : MyCTP<int>
{ }
]]>
</DnaLibrary>
上面的代码不起作用。 Excel-DNA 在 CreateCustomTaskPane
上崩溃与以下 System.Runtime.InteropServices.COMException
:
Unable to create specified ActiveX control
at ExcelDna.Integration.CustomUI.ICTPFactory.CreateCTP(String CTPAxID, String CTPTitle, Object CTPParentWindow)
at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(String controlProgId, String title, Object parent)
at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Type userControlType, String title, Object parent)
at ExcelDna.Integration.CustomUI.CustomTaskPaneFactory.CreateCustomTaskPane(Type userControlType, String title)
[...]
但是,如果父任务 Pane (MyCTP
) 实现了一个接口(interface),它就可以正常工作(请注意,所有类都标记为公共(public)的):
public interface DummyInterface
{ }
public class MyCTP<T> : UserControl, DummyInterface
{ }
我不需要界面,但我想保留模板。有什么想法吗?
最佳答案
我遇到了同样的问题。
解决方案是将 com 可见性传递给 COM(Active X) 对象。
[ComVisible(true)]
public class MyIntCTP : MyCTP<int>
{ }
试试下面的链接,它们会给你一个概述,这会有所帮助。 https://msdn.microsoft.com/en-us/library/aa942861.aspx
查看下面给出的 git 链接以帮助项目。 https://github.com/KevinT/ExcelDna/blob/master/Distribution/Samples/CustomTaskPane.dna
关于c# - Excel-DNA——模板化的 CustomTaskPane 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030009/