我正在尝试使用自定义任务窗格创建 Excel 加载项。我已经按照本教程希望在用户单击功能区上的自定义按钮时显示 Windows 窗体控件。但是,我唯一可以让窗格显示的是当我在This_AddIn_Startup
事件上添加了一个测试消息框时。以下是相关代码:
ThisAddIn.cs
private InputControl myInputControl { get; set; }
private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane {get;set;}
public CustomTaskPane TaskPane
{
get
{
return myCustomTaskPane;
}
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MessageBox.Show("loading"); //If I comment this out, task pane won't show
myInputControl = new InputControl();
try
{
myCustomTaskPane = this.CustomTaskPanes.Add(myInputControl, "Test Task Pane");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
myCustomTaskPane.VisibleChanged +=
new EventHandler(myCustomTaskPane_VisibleChanged);
}
功能区.cs
public partial class Ribbon
{
private void Ribbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void btnPullData_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = true;
}
private void txtPosition_TextChanged(object sender, RibbonControlEventArgs e)
{
}
}
我检查了这个问题并禁用了所有其他加载项,但这仍然不起作用。
在调试时,如果我设置了一个断点btnPullData_Click
并注意到它没有显示时抛出了一个异常。这是两张截图。
窗格加载正确(启用消息框)
窗格未加载(无消息框)
完整的异常文本:
base = {System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Core._CustomTaskPane.get_Window()
at Microsoft.Office.Tools.CustomTaskPaneImpl.get_Window()}
我是一个写 C# 的新手,我该如何解决该异常?我不明白如何添加MessageBox
临时解决问题。
编辑:更改标题