【发布时间】:2017-10-04 20:54:01
【问题描述】:
背景
我目前正在开发 VSTO2015 和 Excel 2016 中的应用程序。该应用程序在不同的窗口中管理多个 CustomTaskPanes。我试图让// some code 在任务窗格打开或关闭时触发。为了处理各种窗口,我实现了一个与此非常相似的结构;
CustomTaskPane in Excel doesn't appear in new Workbooks
ThisAddIn.cs 包含以下类;
public class TaskPaneManager
{
static Dictionary<string, Microsoft.Office.Tools.CustomTaskPane> _createdPanes = new Dictionary<string, Microsoft.Office.Tools.CustomTaskPane>();
/// <summary>
/// Gets the taskpane by name (if exists for current excel window then returns existing instance, otherwise uses taskPaneCreatorFunc to create one).
/// </summary>
/// <param name="taskPaneId">Some string to identify the taskpane</param>
/// <param name="taskPaneTitle">Display title of the taskpane</param>
/// <param name="taskPaneCreatorFunc">The function that will construct the taskpane if one does not already exist in the current Excel window.</param>
public static Microsoft.Office.Tools.CustomTaskPane GetTaskPane(string taskPaneId, string taskPaneTitle, Func<UserControl> taskPaneCreatorFunc)
{
string key = string.Format("{0}({1})", taskPaneId, Globals.ThisAddIn.Application.Hwnd);
string title = taskPaneId;
string windowId = Globals.ThisAddIn.Application.Hwnd.ToString();
if (!_createdPanes.ContainsKey(key))
{
var customTaskPane = taskPaneCreatorFunc();
var pane = Globals.ThisAddIn.CustomTaskPanes.Add(customTaskPane, taskPaneTitle);
_createdPanes[key] = pane;
//
// Set the task pane width as set in the App.Config
//
pane.Width = Convert.ToInt32(ConfigurationManager.AppSettings["TaskPaneWidth"]);
}
return _createdPanes[key];
}
....
我来自Ribbon.cs的电话;
private void btnUploadWizard_Click(object sender, RibbonControlEventArgs e)
{
// Check for configuration sheet first.
string title = "Upload Wizard";
TaskPaneManager.isConfigurationCreated();
var UploadWizardTaskpane = TaskPaneManager.GetTaskPane(title, title, () => new TaskPaneUploadWizard());
UploadWizardTaskpane.Visible = !UploadWizardTaskpane.Visible;
}
问题:事件处理程序
我很难触发事件处理程序。我不能说我做错了什么。在TaskPaneDesigner 中,我使用this.VisibleChanged += new System.EventHandler(this.TaskPaneUploadWizard_VisibleChanged); 附加事件,然后在我的TaskPaneUploadWizard 类中定义它,如下所示;
public partial class TaskPaneUploadWizard : UserControl
{
...
public TaskPaneUploadWizard()
{
InitializeComponent();
}
private void TaskPaneUploadWizard_VisibleChanged(object sender, EventArgs e)
{
// Some code
}
我的想法
在我看来,好像我要么将 eventHandler 附加到 CustomTaskPane 对象以外的其他对象,要么在创建 CustomTaskPane 之前附加它。
救命!
【问题讨论】: